-1

Could any one suggest idea to sort the list accordingly : suppose the List contains :

-SP001 of 2017
-SP002 of 2015
-SP001 of 2015
-SP001 of 2016
-SP005 of 2015
-SP003 of 2015

The the out put should be (List must contain in the below order) :

-SP001 of 2015
-SP002 of 2015
-SP003 of 2015
-SP005 of 2015
-SP001 of 2016
-SP001 of 2017

here i need to sort according to number part as well as year part. I have tried collection sort but it gives out put like :

[SP001 of 2015, SP001 of 2016, SP001 of 2017, SP002 of 2015, SP003 of 2015, SP005 of 2015]
Yassin Hajaj
  • 21,337
  • 9
  • 51
  • 89
Justin
  • 13
  • 5

2 Answers2

0

You can try this: First split the strings and then compare the third element of array.

List<String> list = new ArrayList<>();
list.add("-SP005 of 2015");
list.add("-SP001 of 2017");
list.add("-SP003 of 2015");
list.add("-SP001 of 2015");
list.add("-SP001 of 2016");
list.add("-SP002 of 2015");

Collections.sort(list, new Comparator<String>() {
    public int compare(String o1, String o2) {
        int result = o1.split(" ")[2].compareTo(o2.split(" ")[2]);
        if (result == 0) {// if the years are the same, then compare with first element
            return o1.split(" ")[0].compareTo(o2.split(" ")[0]);
        }
        return result;
    }
});

System.out.println("list = " + list);

And it is the result:

list = [
-SP001 of 2015, 
-SP002 of 2015, 
-SP003 of 2015, 
-SP005 of 2015, 
-SP001 of 2016, 
-SP001 of 2017
]
Bahramdun Adil
  • 5,907
  • 7
  • 35
  • 68
  • I am getting Lambda expressions are allowed only at source level 1.8 or above, I am getting this coz am using jdk 7 – Justin Mar 16 '16 at 08:29
  • Thanks, but i have already tried the above, thats not give me exact answer..List list = new ArrayList<>(); list.add("-SP001 of 2017"); list.add("-SP002 of 2015"); list.add("-SP001 of 2016"); list.add("-SP005 of 2015"); list.add("-SP003 of 2015"); – Justin Mar 16 '16 at 08:48
  • What do you mean by 'not give me exact answer' the result I got from the code I already posted in the answer, it must work. – Bahramdun Adil Mar 16 '16 at 08:51
  • sp01 of 2015,sp02 of 2015,sp01 of 2016,sp03 of 2016,sp01 of 2017, this is the example order i needed, thanks for your answers. – Justin Mar 16 '16 at 08:54
  • OK, then the output is exactly what you want, if you see the answer, it is ordered by year, in ascending order. – Bahramdun Adil Mar 16 '16 at 08:59
  • yes, but for me sorting must not be with year alone... if sp001 of 2015 means need to sort according to 001 and 2015. in your answer 003 is after 005. – Justin Mar 16 '16 at 09:02
  • The answer has been updated again, now it is what you were looking for. Good Luck! – Bahramdun Adil Mar 16 '16 at 09:14
0

For


You may use two Comparator<String> and the method thenComparing within the Collections#sort method

The first comparator will compare the years, while the second will compare the second String and so directly the first part of each one.

Comparator<String> comp1 = (x, y) -> x.split(" ")[2].compareTo(y.split(" ")[2]);
Comparator<String> comp2 = (x, y) -> x.compareTo(y);

Collections.sort(list, comp1.thenComparing(comp2));

System.out.println(list);

Output

[SP001 of 2015, SP002 of 2015, SP003 of 2015, SP005 of 2015, SP001 of 2016, SP001 of 2017]
Yassin Hajaj
  • 21,337
  • 9
  • 51
  • 89