根据多个参数构建排序列表?

时间:2017-09-04 18:01:32

标签: java arrays sorting

我有三个数组

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 th 位置属于同一个实体,例如: -

人[0] ==分数[0] ==钱[0] 杰克有1分和25分钱

我想建立一个列表,按人数按字母顺序排序升序),如果起始字母相同,则应检查积分降序)如果它们也相同,则必须检查降序)。

排序后的最终列表应为{ aruba,alphonso,catnis,hill,james,jack }。

4 个答案:

答案 0 :(得分:3)

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

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>

答案 1 :(得分:3)

如果你做得快而又肮脏:

    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行使用lambdas基于数组索引定义比较器。

以下行使用流:

  1. 创建一个从0到people.length-1
  2. 的int索引流
  3. 根据比较器序列对流的索引进行排序
  4. 将排序后的索引映射到人名
  5. 将其收集到列表中
  6. lambda和溪流不是很酷吗?

答案 2 :(得分:2)

如果您有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));
}

<强>结果:

  

人= [人{姓名=杰克,分数= 1,金钱= 25},人{姓名=詹姆斯,   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}

     

人= [人{姓名=阿鲁巴,积分= 5,金钱= 22},人   {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;
});

答案 3 :(得分:0)

EvanM's answer类似,您应该将三个数据分组到一个类中。

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())
);