• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

secondary sort using Comparator

 
Ranch Hand
Posts: 275
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello. if anybody has a minute, i can't seem to get this sort working. i am sorting vector elements and want to use the date as the primary sort and type as a secondary sort.

i thought i'd try something like this...


String type="widget";

String date_string="25-MAR-2005";

SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy");

Date date_object = dateFormat.parse(date_string);

long time_long=date_object.getTime();


String comparison_string=time_long+type;


then compare the string of time_long+type in the comparator to sort the elements by date and type.

However, my problem comes when the time_long value hits 999 then goes to 1000 say, creating an extra numerical digit. the comparator, since i'm comparing long_time+type strings, will sort the comparison strings with the highest number of time_long digits first, then sort, in order, the lesser number of time_long digits.

For instance, early Sept, 2001 does this...Sept 5 is 999658800000 seconds since 1970...12 digits...while Sept 9 is 1000004400000 seconds from 1970...13 digits....so the sort places Sept 9 entries first, sorts the 13 digit vector elements properly from Sept 9 to the latest date...then goes back to the earliest 12 digit elements and sorts those up through Sept 5. I hope this is understandable...again, i bother to convert these longs to text strings so i can incorporate the Type as the secondary sort. Thank you for reading this.
 
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Then don't mash everything into a string. Basically your comparator should look something like this:

In other words:

1. Compare the two dates. If they are different then return that information. If they are the same then compare the two Strings and return that information instead.
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And you can do this as many times as you need. Simply return the first non-0 difference. You'll only return 0 if all fields you check are equal in comparison.
reply
    Bookmark Topic Watch Topic
  • New Topic