3
\$\begingroup\$

I have the following list to sort:

A 0.53
B 0.56
C 0.56
D 0.98
E 0.33

Please note that my list may contains thousands of such type of records. I am sorting my list and put the sorted list into an array as:

  String str="";
        for(String s: mylist){
            str+=s+",";
        }
        String[] sArr = str.split(",");
        String temp="";
        for(int i=0; i<sArr.length;i++) {
            for(int j= i+1; j<sArr.length;j++){
                if(sArr[i].split("\\s")[1].compareToIgnoreCase(sArr[j].split("\\s")[1])<0){
                    temp= sArr[j];
                    sArr[j]= sArr[i];
                    sArr[i]=temp;
                }
            }
        } 

       //sArr now contains the sorted list

The problem is that it is taking too long to get sorted when I have thousands of records. Is there any other way to perform the same task efficiently in less time, or is there something wrong with my way of coding?

\$\endgroup\$
4
\$\begingroup\$
  • You should split your code up into different methods (this increases readability and makes it possible to profile your code and find bottlenecks). I would create listToArray (which transforms your list to an array, although this isn't really needed), parseInput (which parses A 0.53 to an integer of 0.53, and if you need the original string, you can wrap this up in an object), and sortArray (which performs the actual sorting).
  • your way of transforming a list to an array is not very stable (what if there are , in your strings?) It also doesn't seem very performant. Use the standard solution for this.
  • although I'm not sure why you are even doing this, it seems to just add needless computing time.
  • you seem to be using bubble sort, which has a very bad performance of O(n^2). You should look into other sorting algorithms such as quicksort.
  • you perform split("\\s") multiple times on the same elements. It would be better to perform this once at the beginning to save a lot of computing.
  • have you tried using Collections.sort? I would guess that it will perform better, and the code will definitely be cleaner.
  • be consistent with your spacing, and use more spaces to increase readability (around =, +, etc.

Using Collections.sort your code would look like this:

    // create test list
    List<String> list = new ArrayList<>();
    list.add("B 0.56");
    list.add("E 0.33");
    list.add("A 0.53");

    // sort the list
    Collections.sort(list, (a, b) -> a.split("\\s")[1].compareToIgnoreCase(b.split("\\s")[1]));

    // print sorted list
    for (String list1 : list) {
        System.out.println(list1);
    }

Result:

E 0.33
A 0.53
B 0.56
\$\endgroup\$
  • \$\begingroup\$ Thanks for the reply sir! but how can I use Collections.sort Method for this kind of data I have? \$\endgroup\$ – Java Nerd Mar 27 '15 at 20:54
  • \$\begingroup\$ can you please give me an example for Collections.sort for my data set? \$\endgroup\$ – Java Nerd Mar 27 '15 at 20:54
  • \$\begingroup\$ Maybe (I do not know Java) you can write Collections.sortBy(customComparisonFunction, myData) or something like that. \$\endgroup\$ – Caridorc Mar 27 '15 at 20:58
  • 1
    \$\begingroup\$ @JavaNerd The exact code would be Collections.sort(list, (a, b) -> a.split("\\s")[1].compareToIgnoreCase(b.split("\\s")[1]));. The second argument is a Comparator, I'm using Java 8 lambda to simplify the code. It still performs the parsing twice though, so you should definitely check if the performance increase of doing it just once and wrapping it in an object is worth it. \$\endgroup\$ – tim Mar 27 '15 at 21:00
  • 1
    \$\begingroup\$ @tim This isn't just parsing things twice, it's parsing them as many times as there are comparisons (so \$\mathcal{O}(\log n)\$ times per element). One would use the decorate-sort-undecorate idiom (alt: Schwartzian transform) to fix that. A simpler method would be to use a TreeMap (which is a bit of a hack). It's a shame Java has literally no way to do this nicely. \$\endgroup\$ – Veedrac Mar 29 '15 at 20:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.