We help IT Professionals succeed at work.

manupulation of String order

royjayd
royjayd asked
on
Medium Priority
258 Views
Last Modified: 2012-05-12
hi guys

I have this code in my java


while(rs.next()){      
PeriodVO vo = new PeriodVO();
vo.setMonth(rs.month);
vo.setStatus(rs.status);
vo.setYear(rs.year);

String month= vo.getMonth());  //is a number
String year= vo.getYear());
String periods = MonthUtils.getMonth(month)+"-"+year+" ("+vo.getStatus()+")";
vo.setPeriod(periods);
arraylist.add(vo);
}
so basically finally in my  arrayList i will see data like this in this order

FEB-2011-(CLOSE)
JAN-2010-(OPEN)
JAN-2009-(DISABLE)

My requirment is i want the 'OPEN' status row as my first item.
any idea ?

thanks
Comment
Watch Question

Awarded 2011
Awarded 2011

Commented:


make object extending Comparator which will define
int Compare(String s0, String s1) {
if(s0.indexOff("OPEN") > -1) return 1;

}

and then use this object
in method

Collections.sort(arraylist, new MtComp());

and your arraylist will have Open s your first elemnet
CERTIFIED EXPERT
Top Expert 2016

Commented:
So just sort by status? Please post vo source

Author

Commented:
>>>make object extending Comparator

You mean
Public class PeriodVO extends Comparator
Awarded 2011
Awarded 2011

Commented:
class MyComp2 implements Comparator {

    public int compare(Object o1, Object o2){
        String s1 = (String) o1;
        if(s1.indexOf("OPEN") >-1)return -1;
        else
        return 1;
    }
}

Open in new window


       ArrayList<String>  al =  new ArrayList<String>();
        al.add("FEB-2011-(CLOSE)");
al.add("JAN-2010-(OPEN)");
al.add("JAN-2009-(DISABLE)");

        Collections.sort(al, new MyComp2());

        System.out.println(al);

Open in new window

Output:
[JAN-2010-(OPEN), JAN-2009-(DISABLE), FEB-2011-(CLOSE)]

Open in new window

Awarded 2011
Awarded 2011

Commented:
No it is easier to create a separate class as above - this is testesd and working

Author

Commented:
PeriodVO
private String month;
private String year;
private String status;

set()
get()
Awarded 2011
Awarded 2011

Commented:
This comparator is even better
(previous one genertaed compilation warning, and this one does not):

class MyComp2 implements Comparator<String> {

    public int compare(String o1, String o2){
      
        if(o1.indexOf("OPEN") >-1)return -1;
        else
        return 1;
    }
}

Open in new window

CERTIFIED EXPERT
Top Expert 2016

Commented:
What about the order of the other statuses?
Awarded 2011
Awarded 2011

Commented:

this comparator will make  OPEN as the first
and CLOSE as the last item:
class MyComp2 implements Comparator<String> {

    public int compare(String o1, String o2){

        if(o1.indexOf("OPEN") >-1)return -1;
        else if (o1.indexOf("CLOSE")>-1)return 1;
        else  return 0;
    }
}

Open in new window

CERTIFIED EXPERT
Top Expert 2016

Commented:
>>
this comparator will make  OPEN as the first
and CLOSE as the last item:
>>

How do you know that's the required order. Is it a guess?
Awarded 2011
Awarded 2011

Commented:
Well, it is a guess. Usually if you want OPEN to be first, you may want CLOSE to be the last.
Isn't it logical?

Author

Commented:
>>order of the other statuses?
well it should be OPEN first, CLOSE next, DISABLE third
Awarded 2011
Awarded 2011

Commented:
But if we want DISABLE to be the last, just replacing CLOSE with DISABLE will work
CERTIFIED EXPERT
Top Expert 2016
Commented:
Try the following
class StatusComparator implements Comparator<PeriodVO> {
	final String STATUS_ORDER = "OPENCLOSEDISABLE";
	public int compare(PeriodVO vo1, PeriodVO vo2) {
	    return STATUS_ORDER.indexOf(vo1.getStatus()) - STATUS_ORDER.indexOf(vo2.getStatus());
	}
    }

Open in new window

Awarded 2011
Awarded 2011

Commented:
This will sort in any specified order:

class MyComp2 implements Comparator<String> {

     ArrayList <String> order;

    public MyComp2(){
        order = new ArrayList<String>();
        order.add("OPEN");
        order.add("CLOSE");
        order.add("DISABLE");
    }


    public int compare(String o1, String o2){

        int i1 = -1;
        int i2 = -1;

        for(int j= 0; j<order.size(); j++){
          if(o1.indexOf(order.get(j)) > -1 )i1 = j;
             if(o2.indexOf(order.get(j)) > -1 )i2 = j;


        }

        if(i1 < i2) return -1;
        else if (i1 > i2)return 1;
        else return 0;


    }
}

Open in new window

Awarded 2011
Awarded 2011
Commented:
And of course you can use such comnparator if you want to sort
ArrayList of your PeriodVO elements:

class MyComp2 implements Comparator<PeriodVO> {

     ArrayList <String> order;

    public MyComp2(){
        order = new ArrayList<String>();
        order.add("OPEN");
        order.add("CLOSE");
        order.add("DISABLE");
    }


    public int compare(PeriodVO o1, PeriodVO o2){

        int i1 = -1;
        int i2 = -1;

        for(int j= 0; j<order.size(); j++){
          if(o1.getStatus().indexOf(order.get(j)) > -1 )i1 = j;
             if(o2.getStatus().indexOf(order.get(j)) > -1 )i2 = j;


        }

        if(i1 < i2) return -1;
        else if (i1 > i2)return 1;
        else return 0;


    }
}

Open in new window

Awarded 2011
Awarded 2011

Commented:
If you are retriveing them form database it is in many case better to sort in the sql statement, and
if you are working woth oracle you can use DECODE function (it probably also works on other databases), like that


select * from table order by  decode (status, 'OPEN', 1,'DISABLED',2,'CLOSE',3)

so iit will already come to you in the required order

Author

Commented:
for_yan
you are right, doing it in database is good option(i can just use 'Order by status' and i am good to go). thanks for all the responses, i will close this question shortly.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.