Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / November 2005

Tip: Looking for answers? Try searching our database.

How to sort in a descending way?

Thread view: 
elena.me - 30 Nov 2005 14:11 GMT
Hi,

I have an array which contains several file names..for example:
SBU_2005-11-28_14-38-25.htm
SBU_2005-11-28_14-43-48.htm
SBU_2005-11-28_14-49-10.htm
SBU_2005-11-28_14-54-08.htm
SBU_2005-11-28_14-58-31.htm
SBU_2005-11-28_15-01-57.htm
SBU_2005-11-28_15-04-56.htm
SBU_2005-11-28_15-33-49.htm

I would like to use a method to order this array in a
descending/ascending way?

The only one I found is java.util.Arrays.sort(Files)
but..is there any version for that??

Thanks in advance,
Elena.
VisionSet - 30 Nov 2005 14:28 GMT
> I would like to use a method to order this array in a
> descending/ascending way?
>
> The only one I found is java.util.Arrays.sort(Files)
> but..is there any version for that??

File[] myFiles;

// populate myFiles;

List<File> list = Arrays.asList();

Collections.sort(list);
Collections.reverse(list);

list.toArray(myFiles);

--
Mike W
Thomas Hawtin - 30 Nov 2005 14:47 GMT
>>I would like to use a method to order this array in a
>>descending/ascending way?
[quoted text clipped - 7 lines]
>
> List<File> list = Arrays.asList();
                                  ^myFiles?

> Collections.sort(list);
> Collections.reverse(list);
>
> list.toArray(myFiles);

What's the point of the last line? I suspect you haven't read the
documentation...

Tom Hawtin
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/

VisionSet - 30 Nov 2005 16:10 GMT
> > List<File> list = Arrays.asList();
>                                    ^myFiles?

yes, ooops, but I'm sure the gist was obvious.
it at least points out the API reference which anyone can read

> > Collections.sort(list);
> > Collections.reverse(list);
[quoted text clipped - 3 lines]
> What's the point of the last line? I suspect you haven't read the
> documentation...

I have, it was an oversight, or is that an undersight?

--
Mike W
Bent C Dalager - 30 Nov 2005 17:01 GMT
>> What's the point of the last line? I suspect you haven't read the
>> documentation...
>
>I have, it was an oversight, or is that an undersight?

No, they were comments of a condescending sort.

Cheers
    Bent D
Signature

Bent Dalager - bcd@pvv.org - http://www.pvv.org/~bcd
                                   powered by emacs

Roedy Green - 30 Nov 2005 16:50 GMT
>> I would like to use a method to order this array in a
>> descending/ascending way?

just sort using a comparator that inverts the result of
String.compareTo

See http://mindprod.com/jgloss/comparator.html
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Thomas Hawtin - 30 Nov 2005 14:28 GMT
> I would like to use a method to order this array in a
> descending/ascending way?
>
> The only one I found is java.util.Arrays.sort(Files)
> but..is there any version for that??

For reverse sort, you can supply a comparator that reverses the natural
ordering:

    Arrays.sort(files, Collections.reverseOrder());

Or you could sort it and then reverse it.

Tom Hawtin
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.