Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

How can I sort an array of strings, but not by 1st character?

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Each string is a date in MMDDYYYY format.
I need to sort by YYYY then MM then DD.
Arrays.sort(string)will not work.
Any ideas?
 
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why dont you turn the string around so it is
yyyymmdd ?
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Larry,
I haven't done much myself yet with the Array class, but a quick look at the javadoc index (S for sort) shows
static void (sort Object[] a, Comparator c)
You can define a dateCompare class that implements the Comparator interface type that has the behavior you want in the compare method that has formal parameters like:
int compare(Object o1, Object o2)
the return value, exceptions thrown, and other methods that require implementation (equals method) can be found in javaDoc under the Comparator interface.
Granted, this reply is not a detailed, working example code listing that you can copy/paste into your program, but it has a few hints that might help you in the general direction you might want to go, and I or others can fill in missing details as you discover them.
 
I am Arthur, King of the Britons. And this is a tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic