1

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

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}

But how I will keep the names array with this sort?? result must be: {"Dalal","samah","Mohammad"}

Thanks

5 Answers 5

3

Add a new type that contains both values and implement 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().

Sign up to request clarification or add additional context in comments.

3 Comments

It might at some point make sense to sort people by name. So instead of having Person implement Comparable<...> and it having a fixed implementation. I would prefer to pass a Comparator when sorting: Arrays.sort(people, (x, y) -> Double.compare(x.getScore(), y.getScore()));
That makes more sense than my answer, in fact.
Jorn What do you mean by "people" ?
0

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.

3 Comments

What do you mean by "map" ??
Maps as in hashmap or treemap, these are data structures in java.
Ok, I used TreeMap tm = new TreeMap(); and in for loop i put tm.put(country, new Double(d)); so now i have many of countries and values,,, how i will sort using these values and get their countries?
0

Use TreeMap with keys as the double data and values as the String data. TreeMap is sorted automatically by its keys.

1 Comment

I try this now , I will let you know what will happen
0

Try this. (for 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]
  2. Convert each index to Integer object -> [0, 1, 2]
    (because IntStrem has no sorted(Comparator) method)
  3. Sort each index by values[index] -> [1, 0, 2]
  4. Map each index to names[index] -> ["Dalal", "samah", "Mohamand"]
  5. Save the result as an array -> ["Dalal", "samah", "Mohamand"]

1 Comment

Sorry, it requires Java8.
0

You can also solve this using Comparator. If you want sort based on marks, use PersonMarksComparator, If you want to sort it based on name use 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());
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.