2

I have a String list

ArrayList<String> list = new ArrayList<String>();

with each item:

list.add(week+" "+year);

where week and year are integers.

How to sort this list to ascending order?

2
  • 2
    Please specify, whether you want to sort this list chronologically or alphabetically '52 2009' <=> '10 2010' Commented Oct 21, 2010 at 12:24
  • Hi, I wanna sort chronologically. Commented Oct 21, 2010 at 12:27

10 Answers 10

10

I'll recommend to create a data structure that contains the data you want and implements comparable.

class WeekOfYear implements Comparable<WeekOfYear> {
    private int week ;
    private int year ;
    public WeekOfYear(int week, int year) {
        this.week = week ;
        this.year = year ;
    }
    /**
     * ...
     */
    public int compareTo(WeekOfYear other) {
        int delta = this.year - other.year ;

        return (int)Math.signum(
                delta != 0 ? delta : (this.week - other.week));
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder() ;
        builder.append(week > 9 ? "" : "0") ;
        builder.append(week).append(" ") ;
        builder.append(year) ;

        return builder.toString();
    }
}

List<WeekOfYear> weeks ;
weeks = new ArrayList<WeekOfYear>();
Collections.sort(weeks) ;
Sign up to request clarification or add additional context in comments.

3 Comments

The proper way would seem to be writing your own object, then overriding comparison. This isn't always the easiest or quickest, but if you have the time, it's the right way.
What is with the extra space before the semicolon?
Just an old habit of the times where i started coding, to learn the APIs by name and not rely on syntax highlighting or auto-completion of function names, class name, variable, etc, I've use to code in notepad. Adding the extra space allow me to copy the word without the semicolon both using the mouse and moving left or right with control.
5

You need to write a comparator which splits week and year, compares the year before week (if the year fields are equal). BTW You should put week and year in an object to avoid the splitting.

Comments

3
Collections.sort(list, new Comparator<String>() {
  public int compare(String a, String b) {
    // Your string ordering logic here. (I won't write that for you.)
  }
});

1 Comment

They got me too...not sure why.
2

Implement a Comparator to compare the pieced out week and year values. Use that comparator to sort the list. That should work. Sample implementation is provided below

class WeekYearComparator implements Comparator throws NumberFormatException{
 public int compare(Object weekYr1, Object weekYr2){

 String[] tokens1 = ((String)weekYr1).split(" ");
 int week1 = Integer.parseInt(tokens1[0].trim());
 int year1 = Integer.parseInt(tokens1[1].trim());

 String[] tokens2 = ((String)weekYr2).split(" ");
 int week2 = Integer.parseInt(tokens2[0].trim());
 int year2 = Integer.parseInt(tokens2[1].trim());

 if( year1 > year2)
  return 1;
 else if( year1 < year2)
  return -1;
 else if(year1 = year2){
   if(week1 > week2)
    return 1;
   if(week1 < week2)
    return -1;
   if(week1 == week2)
    return 0;
 }

}

Arrays.sort(list, new WeekYearComparator);

Comments

1
List<String> weeks = new ArrayList<String>();
weeks.add(1 + " " + 1958);
weeks.add(32 + " " + 2007);
weeks.add(32 + " " + 1999);

Collections.sort(weeks, new Comparator<String>()
{
  public int compare(String o1, String o2)
  {
    int y1 = Integer.parseInt(o1.substring(o1.indexOf(" ") + 1));
    int y2 = Integer.parseInt(o2.substring(o2.indexOf(" ") + 1));
    if (y1 == y2)
    {
      int w1 = Integer.parseInt(o1.substring(0, o1.indexOf(" ")));
      int w2 = Integer.parseInt(o2.substring(0, o2.indexOf(" ")));
      return w1 - w2;
    }
    return y1 - y2;
  }
});

System.out.println("weeks = " + weeks);

Comments

0
Arrays.sort(list);

or Arrays.sort(list, Collections.reverseOrder());

3 Comments

OP apparently wants to sort the list chronologically... However, '52 2009' comes alphabetically after '01 2010'.
Then he should have specified so, his question is "how to sort this list". The answer is given.
Hi, sorry that I did not specify preciously, I actually need this list to be sorted chronologically.
0

If you want to sort by chronological order, you're going to have to either swap the year and week so that it's year+" "+week then do a simple alphanumeric sort, or write your own Comparator that accepts two items from the list and reports which one comes first.

Also, consider creating a class that has week and year attributes. Use objects of this class to store your week/year values and then you can output it in any way you want, in addition to having it be easier to sort.

Comments

0

Create a list of Calendar objects using the YEAR and WEEK_OF_YEAR. Sort that, and then convert the result to a list of strings.

Comments

0

If you simply want to sort by the string value (which will be alphabetical order):

Collections.sort(list);

If you wanted to sort chronologically, you could implement a custom Comparator and pass it to the sort method:

public class MyComparator implements Comparator<String> {
    public int compare(String first, String second) {
        // Split the string and compare the ints here
    }

    public bool equals(Object o) {
        return this == o;
    }
}

And then:

Collections.sort(list, new MyComparator());

1 Comment

That will be based alphabetically only. I doubt if that's what the OP wants.
0

General solution:

public static <T> void sort(List<T> list, final List<Comparator<T>> comparatorList) {  
       if (comparatorList.isEmpty()) {//Always equals, if no Comparator.  
            throw new IllegalArgumentException("comparatorList is empty.");  
       }  
       Comparator<T> comparator = new Comparator<T>() {  
       public int compare(T o1, T o2) {  
               for (Comparator<T> c:comparatorList) {  
                   if (c.compare(o1, o2) > 0) {  
                     return 1;  
                   } else if (c.compare(o1, o2) < 0) {  
                     return -1;  
                   }  
               }  
               return 0;  
         }  
       };  
       Collections.sort(list, comparator);  
  }  

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.