Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9061683
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T15:32:54+00:00 2026-06-16T15:32:54+00:00

Possible Duplicate: sort array list of special strings, by date I have an ArrayList

  • 0

Possible Duplicate:
sort array list of special strings, by date

I have an ArrayList of Strings that read in from a file and the first part of each string is a date ex: 12/01/2012. When I use Collections.sort(); it sorts it fine within the year from oldest to newest, but when it gets to 01/01/2013 it throws that up on the top of the list with the oldest. How can I get it to use the year as part of the sort? I tried using Date, but it did the same. Each string has a lot of info I would like to keep in it and just sort it by the beginning. I see no need to post code since it’s working and all I’m having trouble with is just sorting. I’ve searched on here for awhile and tried a few different sorting options to no avail. So I must be confused or forgetting something. If anyone has any ideas how to sort Strings with dates in the beginning please help! Thanks!

example string: “12/01/2012 34023843 Item Number”

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T15:32:56+00:00Added an answer on June 16, 2026 at 3:32 pm

    You need a custom comparator. Comparing strings in natural order (ie, using the fact that String implements Comparable<String>) will give the result you see, since 0 is lower than 1, so this is expected.

    Here is an example class which can do what you want, but note that it assumes all dates are correctly formatted (the parse() method of DateFormat throws an unchecked IllegalArgumentException if the date is incorrect):

    public final class MyComparator
        implements Comparator<String>
    {
        // Date parsing
        // Note: SimpleDateFormat is not thread-safe, if possible use Joda Time instead
        private static final DateFormat fmt = new SimpleDateFormat("dd/MM/YYYY");
    
        private static final Comparator<String> INSTANCE = new MyComparator();
    
        private MyComparator()
        {
        }
    
        public static Comparator<String> getInstance()
        {
            return INSTANCE;
        }
    
        @Override
        public int compare(String o1, String o2)
        {
            // Grab dates
            String s1 = o1.subString(0, 10);
            Date d1 = fmt.parse(s1);
            String s2 = o2.subString(0, 10);
            Date d2 = fmt.parse(s2);
    
            // Date implements Comparable<Date>, so we can use that...
            int ret = d1.compareTo(d2);
    
            // If dates are equal, compare the rest of the strings instead.
            return ret != 0 ? ret : o1.subString(10).compareTo(o2.subString(10));
        }
    }
    

    You can then use Collections.sort(theArray, MyComparator.getInstance());

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: PHP Sort a multidimensional array by element containing date I have some
Possible Duplicate: Sort strings and numbers in Ruby I have an array of place
Possible Duplicate: How to sort an array of javascript objects? I have output that
Possible Duplicate: php sort array by sub-value I have a multidimensional array like the
Possible Duplicate: mysql custom sort I have an array of IDs: $ids = array(5,
Possible Duplicate: Natural Sort Order in C# I have a list with a lot
Possible Duplicate: Sort ArrayList of custom Objects by property I have an arrayList of
Possible Duplicate: How to sort an arraylist of objects java? I have an arraylist
Possible Duplicate: Sort NSArray of date strings or objects I want to sort an
Possible Duplicate: How to sort a NSArray alphabetically? Hi all, I have an array

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.