-5

I am having string list which consists of integer values. i just want to sort in ascending as well as in descending order.

ArrayList<String> a = new ArrayList<String>();
a.add("1435536000000");
a.add("1435622400000");

System.out.println("Before : " + a);
Collections.sort(a, new ComparatorOfNumericString());
System.out.println("After : " + a);

My ComparatorOfNumericString class is -

public class ComparatorOfNumericString implements Comparator<String> {

    @SuppressLint("NewApi")
    @Override
    public int compare(String lhs, String rhs) {
        int i1 = Integer.parseInt(lhs);
        int i2 = Integer.parseInt(rhs);
        return Integer.compare(i1, i2);
    }

}  

Any one having idea how to sort this strings having integer values in java?

Thanks a lot!! in Advance!!

5
  • 1
    do your homework by yourself Commented Jun 26, 2015 at 10:52
  • Use collection class for sorting Commented Jun 26, 2015 at 10:53
  • Why don't you add these strings by converting as Integer in ArrayList? Commented Jun 26, 2015 at 10:53
  • 1
    possible duplicate of How do I sort strings that contain numbers in Java Commented Jun 26, 2015 at 11:07
  • possible duplicate of Sorting string array in Java Commented Jun 26, 2015 at 11:12

3 Answers 3

2

The better way will be to add integers/long in to the ArrayList and then sorting it using Collections.sort(a) method.

If you still want to using String, you will need to make some modifications. Please find below code for the same:

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class Input implements Comparator<String>{
    public static void main(String[] args) {
        ArrayList<String> a = new ArrayList<String>();
        a.add("1435536000000");
        a.add("1435622400000");
        a.add("1");
        a.add("10");
        a.add("20");
        a.add("15");
        a.add("1435622400010");

        System.out.println("Before : " + a);
        Collections.sort(a, new Input());
        System.out.println("After : " + a);
          }

    public int compare(String lhs, String rhs) {
    long i1 = Long.parseLong(lhs);
    long i2 = Long.parseLong(rhs);
    return (int) (i1-i2);
   }

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

3 Comments

@ All- all i need to do is- I have time stamp which consists of long value. I just need to sort this time stamp so that i can see nearest date 1st and longest date belo..like 1435622400000- is Tue, 30 Jun 2015 and 1435536000000 is-Mon, 29 Jun 2015 i want to sort in sch a way that i can see 1st 29 Jun 2015 then 30 Jun 2015
The above example will help you to solve this. If you want to sort in a reverse direction just do (i2-i1) in compare method.
no its not working for me. but thnx for your comment.
1

Try using :

Collections.sort(a);

Without any Comparator. It will sort any String alphabetically so logically it will serve for integer String too.

5 Comments

this case "101" will be larger than "1001", so this sort will be wrong
@VladMatvienko did you solve this issue you mentioned?
@TusharGogna, sure, you just need a custom comparator as a second argument. In it you need to convert values to numbers before comparing them. This way it will compare as expected
@TusharGogna, see abhijeet dhumal answer. It is exactly what I ment
I'm comparing alphanumeric. It is working fine except this bug you mentioned. eg: ABC 3999 is shown smaller than ABC 999. Also, it is not handling upper-lower case. :/
-1

You can use this methods to sorting the Strings.

Collections.sort(arraylist);// for ascending order
Collections.sort(arraylist, Collections.reverseOrder());// for descending order.

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.