简体   繁体   English

如何用另一个字符串数组对double数组进行排序?

[英]How to sort double array with another string array?

I try to sort a double array but with another string array: It means I have this: 我尝试排序一个双数组,但使用另一个字符串数组:这意味着我有这个:

Double[] values = {6.3, 5.5 , 7.0};
String [] names ={"samah","Dalal", "Mohammad"};

samah has 5.5 , Dalal has 6.3 and Mohammad has 7.0 samah5.5Dalal6.3Mohammad7.0

If I want to sort double values, it is simple using this: 如果我想对双值进行排序,使用它很简单:

Arrays.sort(values);

In this case the result is {5.5,6.3,7.0} 在这种情况下,结果是{5.5,6.3,7.0}

But how I will keep the names array with this sort?? 但是我将如何保持这种名称数组? result must be: {"Dalal","samah","Mohammad"} 结果必须是: {"Dalal","samah","Mohammad"}

Thanks 谢谢

Add a new type that contains both values and implement Comparable . 添加包含两个值的新类型并实现Comparable

public class Person implements Comparable<Person> {

  private String name;
  private double score;

  public Person(final String name, final double score) {
    this.name = name;
    this.score = score;
  }

  public String getName() {
      return name;
  }

  public void setName(final String name) {
      this.name = name;
  }

  public double getScore() {
      return score;
  }

  public void setScore(final double score) {
      this.score = score;
  }

  @Override
  public int compareTo(final Person another) {
      return Double.compare(score, another.score);
  }
}

Now an array of Person should order by score when sorted with Arrays.sort() . 现在,当使用Arrays.sort()排序时, Person数组应按分数排序。

Correct me if I am wrong, the objective is to store names and their corresponding values correctly, if that is the case then, 如果我错了,请纠正我,目的是正确存储名称及其相应的值,如果是这样的话,那么,

Why are you using two different arrays ? 你为什么使用两个不同的阵列? Why don't you read values and names in a map? 为什么不在地图中读取值和名称?

If you read them in a map you won't have to face issues of maintaining the order of names and values. 如果您在地图中阅读它们,则不必面对维护名称和值的顺序的问题。 If you do this the need for sorting will be reduced. 如果这样做,将减少对分类的需求。

Use TreeMap with keys as the double data and values as the String data. 使用TreeMap ,将键作为double数据,将值作为String数据。 TreeMap is sorted automatically by its keys. TreeMap由其键自动排序。

You can also solve this using Comparator . 您也可以使用Comparator解决此问题。 If you want sort based on marks, use PersonMarksComparator , If you want to sort it based on name use PersonNameComparator . 如果要根据标记进行排序,请使用PersonMarksComparator ,如果要根据名称对其进行排序,请使用PersonNameComparator It will be very useful if you want to sort it on multiple ways. 如果您想以多种方式对其进行排序,这将非常有用。

class PersonMarksComparator implements Comparator<Person> {

  @Override
  public int compare(Person person1, Person person2) {
      return Double.compare(person1.getMarks(), person2.getMarks());
  }
}

class PersonNameComparator implements Comparator<Person> {

  @Override
  public int compare(Person person1, Person person2) {
      return person1.getName().compareTo(person2.getName());
  }
}

public class Person {

   private String name;
   private double marks;

   public Person(final String name, final double marks) {
      this.name = name;
      this.marks = marks;
   }

   //getters and setters

   public static void main(String[] args) {
      List<Person> persons = new ArrayList<Person>();
      Person person1 = new Person("samah", 5.5);
      Person person2 = new Person("Dalal", 6.3);
      Person person3 = new Person("Mohammad", 7.0);
      persons.add(person1);
      persons.add(person2);
      persons.add(person3);
      Collections.sort(persons, new PersonMarksComparator());
      Collections.sort(persons, new PersonNameComparator());
  }
}

Try this. 尝试这个。 (for Java8) (适用于Java8)

Double[] values = {6.3, 5.5 , 7.0};
String [] names ={"samah","Dalal", "Mohammad"};
String[] sortedNames = IntStream.range(0, names.length)
    .mapToObj(i -> i)
    .sorted(Comparator.comparing(i -> values[i]))
    .map(i -> names[i])
    .toArray(String[]::new);
System.out.println(Arrays.toString(sortedNames));

result: 结果:

[Dalal, samah, Mohammad]

Process: 处理:

  1. Make IntStream of indexes -> [0, 1, 2] IntStream索引的IntStream - > [0,1,2]
  2. Convert each index to Integer object -> [0, 1, 2] 将每个索引转换为Integer对象 - > [0,1,2]
    (because IntStrem has no sorted(Comparator) method) (因为IntStrem没有sorted(Comparator)方法)
  3. Sort each index by values[index] -> [1, 0, 2] 按值[index] - > [1,0,2]对每个索引进行排序
  4. Map each index to names[index] -> ["Dalal", "samah", "Mohamand"] 将每个索引映射到名称[index] - > [“Dalal”,“samah”,“Mohamand”]
  5. Save the result as an array -> ["Dalal", "samah", "Mohamand"] 将结果保存为数组 - > [“Dalal”,“samah”,“Mohamand”]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM