Tuesday 15 March 2011

java - How to sort collection with null's and invert the list afterwards? -


That's why I'm working with the list of dates and some values ​​are "nil". I used the answer to the

  public int (Meriben O1, Mybian O2) from the answer {if (o1.getDate () == faucet) {return (o2.getDate () == faucet)? 0: -1; } If (o2.getDate () == faucet) {return 1; } Return o2.getDate () Compare (o1.getDate ()); }  

To sort both ascending in ascending which first taps

What I want is the ascending order for the first tap and the subsequent versions As in ascending order the value is later. Then when the descending selection is really selected to flip the list, the first values ​​in the IE list are in sequential order, then all the tapes.

After trying to sort the list in ascending order, I try the following code: Collections.reverseOrder (); Taps first and then resolved the date in descending order.

I also tried the archive .reverse (list) . Taps at the end of this list but kept the dates in ascending order.

You can get it modified by a simple comparator according to your custom bean object. -

  The public square dates the operator comparator & lt; Date & gt; {Private Boolean reverse; Public DayContributor (Boolean reverse) {this.reverse = reverse; } Public int comparison (date o1, date o2) {if (o1 == faucet} o2 == faucet) {return o2! = Null? (Reverse 1: -1): (O1! = Null? (Reverse? -1: 1): 0); } The result of int = o1 computer to (o2); Reverse the return? Result * -1: Results; } Public static zero main (string [] args throws ParseException {simple data format dateformat = new simpledate format ("DD-MM-yyyy"); Date [] date = new date [] {empty, date format. PRSE ("10-10-2013"), blank, date format. PRSE ("10-10-2012"), dateformat PRS ("10-10-2015"), dateformetrace ("10-10-2011"), tap}; & Lt; Date & gt; List = array.asList (dates); Collections.sort (list, new datepost (false)); Println (list); collection. Support (list, new date comparter); Println (list); }}  

No comments:

Post a Comment