0

I would like split to some how split each item in the arry and sort it by Id[1] and then date[2]

Been doing a fair amount of reading and cannot seem to figure this one out.

Name,ID,Date
John,432,2012-03-21 04:32:00 AM
Bob,532,2012-01-01 12:12:00 AM
Mike,932,2012-01-11 11:42:00 AM
Mike,932,2012-05-01 12:13:00 AM
John,1532,2012-01-11 07:32:00 AM

package test;

import java.util.ArrayList;
import java.util.Collections;

public class ArraySort {

     public static void main(String args[])
     {
    ArrayList<String> stringList = new ArrayList<String>();
    stringList.add("Bob,532,2012-01-01 12:12:00 AM");
    stringList.add("John,432,2012-03-21 04:32:00 AM");
    stringList.add("Mike,932,2012-05-01 12:13:00 AM");
    stringList.add("John,2532,2012-01-11 11:42:00 AM");
    stringList.add("Mike,1532,2012-01-11 07:32:00 AM");

    Collections.sort(stringList);

    for (int i = 0; i < stringList.size(); i++){
           String item = stringList.get(i);
           System.out.println(item);
        }



    }
}
4
Contributors
3
Replies
14
Views
4 Years
Discussion Span
Last Post by JamesCherrill
0

this and this should give you some ideas.

basically , expanding what stultuske already said :

have a class for comparing your values , it will implement comparator (or comparable) . Hence by rules if interface implementation , you have to override the interface's method : compare(T o1, T o2) in case you choose to use comparator.
then , test for the 1st parameter with which you want to order,
if that results in an equality , test for the 2nd parameter.. and so on.

give it a try.

Edited by somjit{}

0

The "Java" way to do this is to create a tiny class with members for name, id, date. Then use the data to create one instance of that class for each line in the input file. Store those in an ArrayList, then sort as per the previous two posts. Because you will have aleady separated the name,id,date values into separate variables, the comparisons wil be easy.

Edited by JamesCherrill

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.