We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Sort list by length
Page Index Toggle Pages: 1
Sort list by length (Read 1149 times)
Sort list by length
Oct 29th, 2009, 6:07am
 
How can i sort a list by length?

So this
Quote:
such
like
should
so
through


Will get like:

Quote:
through
should
such
like
so
Re: Sort list by length
Reply #1 - Oct 29th, 2009, 6:32am
 
Assuming you haven't found anything suitable in the Reference then I suspect you'll need to rely on Java for that.  This ,%20java.util.Comparator)]Array sort method allows you to pass a Comparator as an argument.  This would be a method that took two string lengths and returned an appropriate argument to the sort method.
Re: Sort list by length
Reply #2 - Oct 29th, 2009, 6:38am
 
Details depend on the exact nature of your list. Array? ArrayList? Something else?

But basically, you have to sort using a Comparator class (or if you define your own class, you can make it to implement Comparable, but that's not the point here).

Here is an example:
Code:
class CompareStringLength implements Comparator
{
int compare(Object o1, Object o2)
{
String s1 = (String) o1;
String s2 = (String) o2;
return s1.length() - s2.length();
}
}

String[] words =
{
"Processing",
"is",
"an",
"awesome",
"software",
"isn't",
"it?"
};

println(words);
Arrays.sort(words, new CompareStringLength());
println(words);

Just invert s1 and s2 in the return clause to sort in the reverse order.
Re: Sort list by length
Reply #3 - Oct 29th, 2009, 6:41am
 
One easy way to do this would be with the Java Collections API and a custom Comparator:

Code:
void setup() {
 ArrayList words=new ArrayList();
 words.add("processing");
 words.add("hi");
 words.add("shuffle");
 words.add("world");
 Collections.sort(words,new LengthComparator());
 for(Iterator i=words.iterator(); i.hasNext();) {
   println(i.next());
 }
}

class LengthComparator implements Comparator {
 public int compare(Object a, Object b) {
   String sa=(String)a;
   String sb=(String)b;
   return sb.length()-sa.length();
 }
}


Using such comparators you can sort any type of object based on one or more properties...
Re: Sort list by length
Reply #4 - Oct 29th, 2009, 6:42am
 
3 people = same idea Smiley
Re: Sort list by length
Reply #5 - Oct 29th, 2009, 6:49am
 
damnit, difficult.

The list is a String[];

The input is from a text file:

String [] eng;
eng = loadStrings("1000_eng.txt");

if someone could give an example with that then that would be really nice.

Re: Sort list by length
Reply #6 - Oct 29th, 2009, 7:07am
 
But it's exactly the same as we've written above. loadStrings() returns an array of Strings, so then just do this:

Code:
void setup() {
String [] eng;
eng = loadStrings("1000_eng.txt");
Arrays.sort(eng, new LengthComparator());
println(eng);
}


(using the LengthComparator class from my reply above)
Re: Sort list by length
Reply #7 - Oct 29th, 2009, 7:53am
 
thank you all so much, specially for the short time.
Re: Sort list by length
Reply #8 - Oct 29th, 2009, 8:58am
 
toxi wrote on Oct 29th, 2009, 6:42am:
3 people = same idea Smiley

Yes, in order of time spent on writing the answer... and testing the given sample! Grin

clankill3r wrote on Oct 29th, 2009, 6:49am:
The list is a String[];

My sample used that...
That's the nice thing with having multiple answers: they addressed slightly different domains.
Page Index Toggle Pages: 1