HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Sorting thousands of records

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
sortingrecordsthousands

Problem

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?

Solution


  • 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 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

Code Snippets

// 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);
    }
E 0.33
A 0.53
B 0.56

Context

StackExchange Code Review Q#85205, answer score: 4

Revisions (0)

No revisions yet.