0

I have arraylist of string as follows

List<String> list = new ArrayList<String>();
list.add("one");
list.add("ten");
list.add("six");
list.add("two");

output after sorting should be
one, two, six, ten

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
ShaileshI
  • 137
  • 1
  • 1
  • 6

3 Answers3

1

You can order based in values of a Map for example:

Map<String, Integer> numbers = new HashMap<>();
numbers.put("one", 1);
numbers.put("two", 2);
numbers.put("three", 3);
numbers.put("four", 4);
numbers.put("five", 5);
numbers.put("six", 6);
numbers.put("seven", 7);
numbers.put("height", 8);
numbers.put("nine", 9);
numbers.put("ten", 10);

List<String> list = new ArrayList<>();
list.add("one");
list.add("ten");
list.add("six");
list.add("two");

Collections.sort(list, (o1, o2) -> {
    return numbers.get(o1) - numbers.get(o2);
});
System.out.println(list);

Output

[one, two, six, ten]
Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
  • 2
    mmm, down-votes and delete why ? can you explain please? – Youcef LAIDANI May 13 '17 at 19:35
  • 2
    Jarrod likes to identify duplicates and then downvote all people daring to answer that duplicated thing. Even when he puts up 4 dups and none of them matches the question asked here. I see what I can do about this. – GhostCat May 13 '17 at 19:39
  • 2
    @GhostCat The fair, always the fair :) – davidxxx May 13 '17 at 19:42
  • 2
    @YCF_L Just for information, I have upvoted your answer after you were dowvoted and just after it someone has still downvoted you. – davidxxx May 13 '17 at 19:44
  • 1
    i want to be all the same @davidxxx :) – Youcef LAIDANI May 13 '17 at 19:46
  • 3
    For the record: I also voted for closing - for being too broad/unclear. Simply because the op only posted requirements but did not show any effort to solve the problem himself. So don't expect me to participate in the reopen vote. I have no problem to re-open wrong dup closes (I actually did that twice during the last weeks), but not today. – GhostCat May 13 '17 at 19:54
  • 1
    @GhostCat Thank you for these details. It is true that some questions don't deserve answers as it is a way to feed vampires and encourage them to come back. But when there is really no fresh Java questions, I make too often the mistake to answer. I have to work on it :) – davidxxx May 13 '17 at 20:00
1

Quick and short solution:

List<String> allNumbers = Arrays.asList("one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten");
Collections.sort(list, (i1, i2) -> allNumbers.indexOf(i1) - allNumbers.indexOf(i2));

Use a HashMap with indexed string keys for a faster algorithm (if you have 10 values, there's no difference anyway).

steffen
  • 16,138
  • 4
  • 42
  • 81
1

Java doesn't provide an out of box way to sort String in this ways.
You could use a Comparator that compare the value of two String according to this rule.

An effective way would be to create in the Comparator class with all String that you want to sort and use the index of the element to know its order.

For example :

public class MyNumberInStringComparator implements Comparator<String> {

    private static List<String> numbers = Arrays.asList("one", "two", "three");             


    @Override
    public int compare(String o1, String o2) {
        return Integer.compare(numbers.indexOf(o1),numbers.indexOf(o2));
    }

}
davidxxx
  • 125,838
  • 23
  • 214
  • 215