A: 

Wouldn't sorting them take care of this?

Michael McCarty
The English dictionary is sorted, but you don't get all the 1-letter words before all the 2-letter words
Gareth
True, but in the sample data given a sort would work.
Michael McCarty
+3  A: 

Sort-by-length, using a sort function:

urls.sort(lambda a, b: cmp(len(a), len(b)))

For performance, some might prefer the decorate-sort-undecorate pattern:

urllengths= [(len(url), url) for url in urls]
urllengths.sort()
urls= [url for (l, url) in urllengths]

Or as a one-liner:

urls= zip(*sorted((len(url), url) for url in urls))[1]
bobince
jython supports a `key` argument for `sort()`, so you could just use: `urls.sort(key=len)`.
J.F. Sebastian
only supported since jython-2.5 not on the 2.2 series
Chmouel Boudjnah
+1  A: 

Until jython catches up to python 2.4, you cannot use the key argument to list.sort():

mylist.sort(key=len)

So, like in the good old days, we have the decorate-sort-undecorate idiom. To sort mylist by item length, we generate a decorated_list of (len(item),item) tuples, sort that, and finally strip the items back:

decorated_list = zip(map(len, mylist), mylist)
decorated_list.sort()
sorted_list = [i[1] for i in decorated_list]
gimel
For the symmetry I would use: `sorted_list = map(operator.itemgetter(1), decorated_list)` instead of `sorted_list = [i[1] for i in decorated_list]`
J.F. Sebastian
Or (for symmetry): decorated_list = zip([len(i) for i in mylist], mylist)(Jython 2.2)
gimel
Jython 2.2 doesn't have operator.itemgetter().
J.F. Sebastian