3

So I have a List<String> L1 = new ArrayList<>() with the following strings as elements:

  • l, l, u, u.
  • r, u, d, l, d, l, u.
  • l, u, d, r, r, r, r, r, u, d.
  • l, u.
  • l, u, r.

How do I sort the list by its element's size so that in the final state L1 should be like this:

  • l, u.
  • l, u, r.
  • l, l, u, u.
  • r, u, d, l, d, l, u.
  • l, u, d, r, r, r, r, r, u, d.

I have tried using Collections.sort, but that sorts alphabetically, which is obviously not what I want.

OPK
  • 4,120
  • 6
  • 36
  • 66
miTzuliK
  • 93
  • 1
  • 1
  • 11

3 Answers3

8

I think what you are looking for is Collections.sort(java.util.List, java.util.Comparator). With it you can specify a custom Comparator that compares the strings based on length instead of alphabetical relationship.

Something like this:

List<String> stringList = new ArrayList<String>();

// Fill the list

Comparator<String> stringLengthComparator = new Comparator<String>()
    {
        @Override
        public int compare(String o1, String o2)
        {
            return Integer.compare(o1.length(), o2.length());
        }
    };

Collections.sort(stringList, stringLengthComparator);
curob
  • 705
  • 4
  • 17
5

Assuming Java 8 is available, and also that the list is of type List<String>:

list.sort(Comparator.comparing(String::length));

This creates a new Comparator that uses the length of the string as the basis for sorting, then sorts according to it. The list is sorted from short to long, but if that's undesirable...

list.sort(Comparator.comparing(String::length).reversed());

...would invert it to sort the longest strings first.

If the list is actually a list of lists (the question is slightly unclear on that), List::size would be used instead of String::length.

BambooleanLogic
  • 7,530
  • 3
  • 30
  • 56
  • Let me get this straight, regarding i've never used Comparators before. So, all I have to do for sorting my L1 list as I said before, after element's size, is this: L1.sort(Comparator.comparing(String::length)); ? – miTzuliK Mar 26 '15 at 14:11
  • Yes, again assuming Java 8 and that you're sorting `String` instances. – BambooleanLogic Mar 26 '15 at 14:12
  • I hate to revive an old thread but can this be done with comparable and compareTo? – blueGOLD Jan 17 '17 at 19:23
  • Using `Comparator.comparingInt` would be more appropriate than `Comparator.comparing` – Marc Sep 08 '20 at 08:05
0

You can use any sorting technique(bubble,insertion etc.) to sort the elements of Arraylist.Here I use bubble sort to sort the elements of Arraylist.

public class SortArray
{
    public static void main(String[] args)
    {   
        List<String> al = new ArrayList<String>();
        //add items to arraylist objects.
        al.add("l,l,u,u");
        al.add("r, u, d, l, d, l, u");
        al.add("l, u, d, r, r, r, r, r, u, d");
        al.add("l,u");
        al.add("l,u,r");

        //Use bubble sort here.
        for(int i=0;i<al.size();i++){
            for(int j=0;j<al.size()-i-1;j++){
                if( (al.get(j)).length() > (al.get(j+1)).length() ){
                    Collections.swap(al, j, j+1);
                }
            }
        }

        for(String str : al){
            System.out.println(str);
        }

    }
}
Aditya
  • 1,214
  • 7
  • 19
  • 29
  • That's a big coincidence, because that's what I actually did, about half an hour ago. I'm in a time crysis, that's why I did the way I could. I know Smallhackers' solution is the best (because of avoiding that O(n^2), those 2 fors from bubble)... – miTzuliK Mar 26 '15 at 15:11
  • While your answer may be technically correct, bubblesort is pretty much NEVER a good idea, with perhaps the sole exception being as a case study in how not to sort data (together with bogosort and others). Furthermore, Java's built in sorting system should be sufficient for most uses. Unless you know what you're doing, or have very specific requirements, implementing your own sorting algorithm (as opposed to using Java's or a sorting library's) is usually not the answer. – BambooleanLogic Mar 26 '15 at 15:26