-2

I want to sort a List of Strings in descending order.

I tried different comparators but none is giving the answer i am looking for.

public class arrasss {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        List<String> a1 = Arrays.asList("7560923143", "7560923053", "7560922899", "7560922344", "7560504628",
                "7280202486", "7280202087", "7280202068", "353549090116186", "353549090116079", "353549090114991",
                "353549090113191", "353549090112896", "353549090000007");

        a1.sort(Comparator.reverseOrder());
        // Collections.reverse(a1);

        System.out.println(a1);

    }

}

Received Answer: [7560923143, 7560923053, 7560922899, 7560922344, 7560504628, 7280202486, 7280202087, 7280202068, 353549090116186, 353549090116079, 353549090114991, 353549090113191, 353549090112896, 353549090000007]

Expected Answer : 353549090116186 353549090116079 353549090114991 353549090113191 353549090112896 353549090000007 7560923143...

GalAbra
  • 5,048
  • 4
  • 23
  • 42
Arun Nair
  • 425
  • 3
  • 11

2 Answers2

-1

Wrong type: Long, not String

Strings sort in alphabetical order, comparing first letter to first letter. In a tie, the second letters are compared. And so on.

For numerical sorting you need numbers. Convert your strings to numbers.

Long myLong = Long.valueOf( "353549090000007" ) ;

If typing these numbers into your source code, use literals.

In a numeric literal, feel free to type an underscore anywhere you’d like. The underscore characters are ignored by Java. Their presence is strictly to aid human reading.

Long myLong = 353_549_090_000_007L ;

In that line above, notice the auto-boxing feature of Java in action. On the right side, we create a primitive long value. That primitive is automatically used to instantiate a Long object.

Define your inputs with the convenient List.of method.

List< String > inputs = List.of("7560923143", "7560923053", "7560922899", "7560922344", "7560504628",
            "7280202486", "7280202087", "7280202068", "353549090116186", "353549090116079", "353549090114991",
            "353549090113191", "353549090112896", "353549090000007");

Define a list of Long to collect our objects converted from these strings. Match the size to the inputs list.

int initialCapacity = inputs.size() ;
List< Long > longs = new ArrayList<>( initialCapacity ) ;

Use the for-each syntax to loop the inputs. Parse each input String as a Long.

for( String input : inputs ) {
    Long x = Long.valueOf( input )  ;
    longs.add( x ) ;
}

To sort, no need for a Comparator. The Long class knows how to sort, having implemented the Comparable interface.

Sort using the Collections class’ utility methods.

Collections.reverseOrder( longs ) ;
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
-1

Since you've tagged Java 8, you can also consider streams. The expression below does reverse numerical ordering by parsing the strings as longs.

List<String> a = 
  Stream.of("7560923143", "7560923053", "7560922899", "7560922344", "7560504628",
    "7280202486", "7280202087", "7280202068", "353549090116186", "353549090116079",
    "353549090114991", "353549090113191", "353549090112896", "353549090000007")
      .sorted(Comparator.<String>comparingLong(Long::parseLong).reversed())
      .collect(toList());

Gene
  • 46,253
  • 4
  • 58
  • 96