• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

sort by two fields

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
I will like to sort an object having two instance variables(location and event date).

Location can be blank. I will like all objects with blank locations to appear at the top of the list, sorted by event date.

How do I do this?

What I did was implemet Comparator and pass it to "Collections.sort" along with the list. I was able to get records without value for location to appear at the top
by returning 1 from the compare method if either object being compare has a blank location but I'm unable to sort the blank location records by event date.

Below is the compare method
 
Sheriff
Posts: 22815
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Obviously that's not your entire code; after all, this method won't compile.

How comparing on multiple fields usually works is as follows:
Note: you can nest the ifs, but that may cause nesting ifs too deeply

In your case, the first comparison is somewhat like this:
You may feel uncomfortable with using nested tertiary operators; you can use simple if statements instead. All that matters is the following matrix:
- blank1 && blank2: both are blank so 0
- blank1 && !blank2: only the first one is blank, so the first one goes before the second one. -1
- !blank1 && blank2: only the second one is blank, so the second one goes before the first one. 1
- !blank1 && !blank2: neither is blank so compare the two
 
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another approach would be to add a key or index attribute (on the way in
to the collection) that combines the other attributes in proper order. Then
a simple one field sort could be used.

Jim...
 
Moses Marfo
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks Rob for the solution
the list is being sorted properly now

Jim ........can you please expound on the alternative solution
 
Jim Hoglund
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moses: How many location/eventDate pairs are you working with,
do you have a storage structure in mind, and how dynamic is the
list; how frequently are pairs added, changed or removed? It would
be helpful to know a bit more about the context of your questions?

Jim... (and greetings to Rob, our fine moderator.)
 
Moses Marfo
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jim
The program is an event audit report which allows event records retrieved from database to be sorted based on entered creteria.
The Object that I'm using to store the retrieved records consist of more than just location and event date, there is also user name and event description
The List of records consist of two lists obtained from database using two separate queries. The number of records in the list can range from 0 - 1000 or more.
The list is not updated once retrieved from the database, it is just for display purposes. I hope that is sufficient information.
 
Paper jam tastes about as you would expect. Try some on this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic