基于多个参数构建排序列表?

z5btuh9x  于 2021-07-03  发布在  Java
关注(0)|答案(4)|浏览(258)

我有三个数组

String[] persons = {"jack","james","hill","catnis","alphonso","aruba"};
int[] points = {1,1,2,3,4,5};
int[] money = {25,66,24,20,21,22};

所有三个数组中的第n个位置属于同一个实体,例如:-
人[0]==分[0]==钱[0],即杰克有1分和25美元。
我想建立一个名单,排序的人按字母顺序(升序),如果开始字母是相同的,那么它应该检查点(降序),如果这些是相同的,那么它必须检查钱(降序)。
排序后的最终列表应该是{aruba,alphonso,catnis,hill,james,jack}。

xytpbqjk

xytpbqjk1#

如果你想干些又快又脏的事:

Comparator<Integer> cName = (i, j) -> Character.compare( persons[i].charAt(0), persons[j].charAt(0));
    Comparator<Integer> cPoints = (i, j) -> Integer.compare( points[i], points[j]);
    Comparator<Integer> cMoney = (i, j) -> Integer.compare( money[i], money[j]);

    List<String> l = 
            IntStream.range(0, persons.length).boxed()
            .sorted( cName.thenComparing(cPoints.reversed()).thenComparing(cMoney.reversed()) )
            .map( i -> persons[i] )
            .collect(Collectors.toList());

    System.out.println(l);

前3行使用lambda根据数组索引定义比较器。
以下行使用流:
创建从0到persons.length-1的索引的int流
基于比较器序列对流的索引进行排序
将排序索引Map到人名
将其收集到列表中
lambda和streams不是很酷吗?

qfe3c7zg

qfe3c7zg2#

所以我想你想要这样的东西:

public class Person {
   String name;
   int points;
   int money;

   public Person(String name, int points, int money) {
       this.name = name;
       this.points = points;
       this.money = money;
   }

   // getters
}

然后创建一个 List<Person> 使用您拥有的数据(例如。, new Person("jack", 1, 25) ). 然后对它们进行排序:

Collections.sort(persons, (person1, person2) -> {
    // could be written more concisely, but this should make things clear
    char letter1 = person1.getName().charAt(0);
    char letter2 = person2.getName().charAt(0);
    if (letter1 != letter2) {
        return letter1 - letter2;
    }
    int points1 = person1.getPoints();
    int points2 = person2.getPoints();
    if (points1 != points2) {
        return points2 - points1; // notice the order is reversed here
    }
    int money1 = person1.getMoney();
    int money2 = person2.getMoney();
    if (money1 != money2) {
        return money2 - money1;
    }
    return 0; // unless you want to do something fancy for tie-breaking
});

那会给你一个好印象 List<Person> 根据你的标准。

aelbi1ox

aelbi1ox3#

与Evam的答案类似,您应该将这三个数据分组到一个类中。

public class Person {
   private String name;
   public String getName() { return name; }

   private int points;
   public int getPoints() { return points; }

   private int money;
   public int getMoney() { return money; }
}

然后你可以这样分类:

List<Person> persons = ...;

persons.sort(Comparator
    .comparing(p -> p.getName().charAt(0))
    .thenComparing(Comparator.comparing(Person::getPoints).reversed())
    .thenComparing(Comparator.comparing(Person::getMoney) .reversed())
);
7rtdyuoh

7rtdyuoh4#

如果你能有一个 Person 型号:

final class Person {
  private final String name;

  private final int points;

  private final int money;

  public Person(final String name, final int points, final int money) {
    this.name = name;
    this.points = points;
    this.money = money;
  }

  // getters and setters (if you want)

  @Override
  public String toString() {
    final StringBuffer sb = new StringBuffer("Person {")
        .append("name=")
        .append(name)
        .append(", points=")
        .append(points)
        .append(", money=")
        .append(money)
        .append('}');
    return sb.toString();
  }
}

然后你可以这样做:

public static void main(final String... args) throws Exception {
  Person[] persons = new Person[6]; // Use a List (if you can)
  persons[0] = new Person("jack", 1, 25);
  persons[1] = new Person("james", 1, 66);
  persons[2] = new Person("hill", 2, 24);
  persons[3] = new Person("catnis", 3, 20);
  persons[4] = new Person("alphonso", 4, 21);
  persons[5] = new Person("aruba", 5, 22);
  System.out.printf("persons = %s%n%n", Arrays.toString(persons));
  System.out.printf("Person[0] = %s%n%n", persons[0]);
  Collections.sort(Arrays.asList(persons), (c1, c2) -> {
    final int charComp = Character.compare(c1.name.charAt(0), c2.name.charAt(0));
    if (0 == charComp) {
      final int pointsComp = Integer.compare(c2.points, c1.points);
      if (0 == pointsComp) { return Integer.compare(c2.money, c1.money); }
      return pointsComp;
    }
    return charComp;
  });
  // The collection was modified at this point because of the "sort"
  System.out.printf("persons = %s%n", Arrays.toString(persons));
}

结果:
persons=[person{name=jack,points=1,money=25},person{name=james,points=1,money=66},person{name=hill,points=2,money=24},person{name=catnis,points=3,money=20},person{name=alphonso,points=4,money=21},person{name=aruba,points=5,money=22}]
person[0]=person{name=jack,points=1,money=25}
persons=[person{name=aruba,points=5,money=22},person{name=alphonso,points=4,money=21},person{name=catnis,points=3,money=20},person{name=hill,points=2,money=24},person{name=james,points=1,money=66},person{name=jack,points=1,money=25}]
更紧凑的 sort (但效率稍低,因为您必须提前运行所有比较):

Collections.sort(Arrays.asList(persons), (c1, c2) -> {
  final int names = Character.compare(c1.name.charAt(0), c2.name.charAt(0));
  final int points = Integer.compare(c2.points, c1.points);
  final int money = Integer.compare(c2.money, c1.money);
  return (0 == names) ? ((0 == points) ? money : points) : names;
});

相关问题