How to sort line number in descending order?

Below is my list, which will always be in shape bXXXX-

List<String> data = Arrays.asList("b5", "b10", "b2", "b1", "b40");
System.out.println(data);

There datawill always be lines in the form in my listbXXXX

Now I need to sort my lists above in descending order and extract the largest row from it.

So in the example above it will be b40. So below is a comparator that sorts everything in ascending order -

static final Comparator<String> aStringComparator = new Comparator<String>() {
    public int compare(String s1, String s2) {
        //assumed input are strings in the form axxxx
        int numfromS1 = Integer.valueOf(s1.subSequence(1, s1.length()).toString());
        int numfromS2 = Integer.valueOf(s2.subSequence(1, s2.length()).toString());
        return numfromS1 - numfromS2;
    }
};

And I can't figure out how to do this, to sort it in descending order, so that I can extract the first index from the list, which will be the largest row number?

+3
source share
4 answers

, , , .

:

return numfromS1 - numfromS2;

:

return numfromS2 - numfromS1;

, , :

return (numfromS2 < numfromS1) ? -1: (numfromS2 > numfromS1) ? 1 : 0;

.

, , :

return ((Integer) numfromS2).compareTo(numfromS1);

Collections.sort(data, Collections.reverseOrder(aStringComparator));
+1

:

    Collections.sort(data, new Comparator<String>() {
        @Override
        public int compare(String s1, String s2) {
            int n1 = Integer.parseInt(s1.substring(1));
            int n2 = Integer.parseInt(s2.substring(1));
            return Integer.compare(n2, n1);
        }});
+1

Use this to sort in descending order -

Collections.sort(data, Collections.reverseOrder(aStringComparator));
+1
source

def Descending_Order (num):> # num = 18902

l = list(map(int, str(num)))            >  # [1, 8, 9, 0, 2]

r = sorted(l,reverse=True)              > # [9, 8, 2, 1, 0]

ch_r = map(str, r)                      > # ['9', '8', '2', '1','0']

num_r =''.join(ch_r)                    > # '98210'

return int(num_r)                       > # 98210
0
source

All Articles