Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 402
  • Last Modified:

How to Sort a Vector of HashMaps

hi,

i have a Vector of HashMap and the HashMap contains different data types:

Vector<HashMap<String, Object>> theVector= new Vector<HashMap<String, Object>>();

theResults contains this HashMap:
HashMap<String, Object> theHashMap= new HashMap<String, Object>();

theHashMaphas these data:
(pretend this is a for-loop)
//1st set<br>

theHashMap.put("BLDG_ID", 111);//int<br>
theHashMap.put("EMP_NAME", "AAA");//String<br>
theHashMap.put("FLAG", true);//boolean<br>
<br>
<br>
theVector.add(theHashMap);
<br>
<br>

//2nd set<br>

theHashMap.put("BLDG_ID", 222);//int<br>
theHashMap.put("EMP_NAME", "BBB");//String<br>
theHashMap.put("FLAG", false);//boolean<br>
<br>
<br>
theVector.add(theHashMap);<br>
<br>

//2nd set<br>
theHashMap.put("BLDG_ID", 111);//int<br>
theHashMap.put("EMP_NAME", "CCC");//String<br>
theHashMap.put("FLAG", false);//boolean<br>
<br>
<br>

theVector.add(theHashMap);<br>
<br>
<br>

i want to sort the contents of my vector of HashMap according to BLDG_ID so that when i display the data it would look like<br>

BLDG_ID || EMP_NAME<br>
111     ||    AAA<br>
111     ||    CCC<br>
222     ||    BBB<br>


unfortunately i cannot change the data types because changing it would break a  lot of functionalities and dependencies

i tried doing :

Collections.sort ( theVector, new HashMapComparator () ) ;

and I have HashMapComparator class (see below) but I can't use Collections.sort for theVector

public class HashMapComparator {
      boolean flag = false ;

    public int compare ( Object object1 , Object object2 )
    {
        if ( flag == false )
        {
            Integer obj1Value = ( Integer ) ( ( HashMap ) object1 ).get ( "BLDG_ID" ) ;
            Integer obj2Value = ( Integer ) ( ( HashMap ) object2 ).get ( "BLDG_ID" ) ;
           
            return obj1Value.compareTo ( obj2Value ) ;
        }
        else
        {
            Integer obj1Value = ( Integer ) ( ( HashMap ) object1 ).get ( "BLDG_ID" ) ;
            Integer obj2Value = ( Integer ) ( ( HashMap ) object2 ).get ( "BLDG_ID" ) ;
           
            return obj2Value.compareTo ( obj1Value ) ;
        }
    }
}


thanks in advance!

0
ayeen
Asked:
ayeen
  • 15
  • 15
  • 2
2 Solutions
 
for_yanCommented:
So what's your question?
Does your sorting work?
0
 
for_yanCommented:
I'm not sure I undersatdn why do you need flag - you just find the value for "BLDG_ID in each hashtable
and comapre them - please, explain why do you need flag?
0
 
for_yanCommented:
flag is a part of the HashMap - so if its false - you want to put these HashMaps down lthe list?
hy don;t you get this flag from intances of Object1 and Ibject2 in the method then?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
ayeenAuthor Commented:
no, that sorting doesn't work..
0
 
ayeenAuthor Commented:
my apologies... just please ignore the HashMapComparator i just posted that just in case someone would post that same answer from another thread that somewhat has the same problem as mine (his is List mine is Vector)
0
 
ayeenAuthor Commented:
my problem is how to sort a vector of hashmaps ..thanks!
0
 
for_yanCommented:
Don't understand about posting HashMapComparator

But HashMapComparator is a good way to go

Just remove the stuff about the flag there and compare two integers which correspodn to BUILD_ID in each of the HshMaps

If that does not work - can you post your code - and I'll try to debug
0
 
ayeenAuthor Commented:
here's the snippet of the code:

private void getResults(HttpServletRequest request)
{
    Vector<HashMap<String, Object>> theVector = new Vector<HashMap<String, Object>>();
   
    //get all "hit" values
    while(aciHit != null)
    {
         HashMap<String, Object> theHit = new HashMap<String, Object>();
         theHashMap.put("BLDG_ID", intVal);//int<br>
          theHashMap.put("EMP_NAME", stringVal);//String<br>
          theHashMap.put("FLAG", booleanVal);//boolean<br>

          theVector .add(theHit);

          aciHit = aciHit.next();//fetch next
    }//end while
   
    //when we're done getting all values we:
    // (1) send theVector to process A
    // (2) sort theVector before sending to process B (process B displays the values by Building Id)

    Collections.sort ( theResults , new HashMapComparator () ) ;//compiler error
   
   
}

the line:

Collections.sort ( theVector , new HashMapComparator () ) ;

is giving me:

The method sort(List<T>, Comparator<? super T>) in the type Collections is not applicable for the arguments (Vector<HashMap<String,Object>>, HashMapComparator)




the updated  HashMapComparator:

public class HashMapComparator
{

    public int compare ( Object object1 , Object object2 )
    {
             Integer obj1Value = ( Integer ) ( ( HashMap ) object1 ).get ( "BLDG_ID" ) ;
            Integer obj2Value = ( Integer ) ( ( HashMap ) object2 ).get ( "BLDG_ID" ) ;
           
            return obj1Value.compareTo ( obj2Value ) ;
     }
}
0
 
for_yanCommented:
This is working for me:

import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Vector;

public class SortVectorOfHashMaps {

    public static void main(String[] args) {


Vector<HashMap<String, Object>> theVector= new Vector<HashMap<String, Object>>();


HashMap<String, Object> theHashMap= new HashMap<String, Object>();

//theHashMaphas these data:
//(pretend this is a for-loop)
//1st set<br>

theHashMap.put("BLDG_ID", 333);//int<br>
theHashMap.put("EMP_NAME", "AAA");//String<br>
theHashMap.put("FLAG", true);//boolean<br>
theVector.add(theHashMap);

//2nd set<br>
 theHashMap= new HashMap<String, Object>();

theHashMap.put("BLDG_ID", 222);//int<br>
theHashMap.put("EMP_NAME", "BBB");//String<br>
theHashMap.put("FLAG", false);//boolean<br>
//<br>
//<br>
theVector.add(theHashMap);
//<br>
theHashMap= new HashMap<String, Object>();
//2nd set<br>
theHashMap.put("BLDG_ID", 111);//int<br>
theHashMap.put("EMP_NAME", "CCC");//String<br>
theHashMap.put("FLAG", false);//boolean<br>



theVector.add(theHashMap);

  Collections.sort(theVector, new HashMapComp());

        for(HashMap<String, Object> h : theVector){
            System.out.println(h);
        }

        
    }


}

class HashMapComp implements Comparator <HashMap<String, Object>> {

    public int compare (  HashMap<String, Object> object1 ,  HashMap<String, Object> object2 )
      {

              Integer obj1Value = ( Integer ) ( ( HashMap ) object1 ).get ( "BLDG_ID" ) ;
              Integer obj2Value = ( Integer ) ( ( HashMap ) object2 ).get ( "BLDG_ID" ) ;

              return obj1Value.compareTo ( obj2Value ) ;

      }
  }


    

Open in new window



{EMP_NAME=CCC, BLDG_ID=111, FLAG=false}
{EMP_NAME=BBB, BLDG_ID=222, FLAG=false}
{EMP_NAME=AAA, BLDG_ID=333, FLAG=true}

Open in new window

0
 
CEHJCommented:
>>
The method sort(List<T>, Comparator<? super T>) in the type Collections is not applicable for the arguments (Vector<HashMap<String,Object>>, HashMapComparator)
>>

You need

public static class HashMapComparator implements Comparator<Map<String, Object>> {
        public int compare(Map<String, Object> m1, Map<String, Object> m2) { 
            Integer obj1Value = (Integer) m1.get("BLDG_ID");
            Integer obj2Value = (Integer) m2.get("BLDG_ID");

            return obj1Value.compareTo(obj2Value);
        }
    }

Open in new window

0
 
ayeenAuthor Commented:
i'm trying the code snippets above it worked but when im trying to use it on real live data i'm getting null pointer exception i'm guessing it's because there are null BLDG_ID in the live data

is there a way on handling null values when using Comparator?

0
 
for_yanCommented:
Sure
add  like that :
if(( ( HashMap ) object1 ).get ( "BLDG_ID" ) == null) return -1;
if(( ( HashMap ) object2 ).get ( "BLDG_ID" ) == null) return 1;

before:

 Integer obj1Value = ( Integer ) ( ( HashMap ) object1 ).get ( "BLDG_ID" ) ;
              Integer obj2Value = ( Integer ) ( ( HashMap ) object2 ).get ( "BLDG_ID" ) ;

              return obj1Value.compareTo ( obj2Value ) ;

where do you want ull numbers to end up - in th end?

0
 
for_yanCommented:
Maybe you want to reverse:

if(( ( HashMap ) object1 ).get ( "BLDG_ID" ) == null) return 1;
if(( ( HashMap ) object2 ).get ( "BLDG_ID" ) == null) return -1;

it is bess to see by trying - wejhere would you see nulls and if you don't like it then reverse


0
 
ayeenAuthor Commented:
thanks i'll try that.

also, is it possible to sort using two key values?

like what if i need to sort the BLDG_ID and the EMP_NAME so that the results would be something like:

BLDG_ID   EMP_NAME
111            AAA
111            CCC

222             YYY  

333            BB
333            ZZZ

thanks again
               
0
 
ayeenAuthor Commented:
it's like the EMP_NAME is my secondary sort
0
 
for_yanCommented:
sure - make sure it works the way it is now - after that you'll just need to modify the comaparator
and add something,
like if first compare is 0, then go retiries the second elemetnt and compare them.
Test, that it is woroking niow, it will be not difficult to cvhange when it works
0
 
for_yanCommented:
Something like that:
(but rather test stepwise - first like we did before - and then modify to this)

class HashMapComp implements Comparator <HashMap<String, Object>> {

    public int compare (  HashMap<String, Object> object1 ,  HashMap<String, Object> object2 )
      {

              Integer obj1Value = ( Integer ) ( ( HashMap ) object1 ).get ( "BLDG_ID" ) ;
              Integer obj2Value = ( Integer ) ( ( HashMap ) object2 ).get ( "BLDG_ID" ) ;

             int first = obj1Value.compareTo ( obj2Value );

      if(first == 1) return 1;
   else if(first == -1) retirn -1;
  else
{

if(( ( HashMap ) object1 ).get ( "EMP_NAME" ) == null) return -1;
if(( ( HashMap ) object2 ).get ( "EMP_NAME" ) == null) return 1;


    String obj1Str = ( Integer ) ( ( HashMap ) object1 ).get ( "EMP_NMAE" ) ;
              Stringr obj2Str = ( Integer ) ( ( HashMap ) object2 ).get ( "EMP_NAME" ) ;

   return obj1Str.compareTo ( obj2Str );


}

             return 0;
      }
  }

Open in new window

0
 
ayeenAuthor Commented:
wow! that worked great! thank you. i had to remove return 0; though coz its giving me compiler error (unreachable code)

the only problem i have now is when it's already sorted it sorts in reverse... like if the data in the hashmap is already in descending order and i call the code above it reverses into ascending....is there a way to force it to stay in descending when the data being sorted is already in descending order?

here's another thing i noticed, when i added another key to sort (EMP_ID...i changed the EMP_NAME) it didnt reverse the sorting of the BLDG_ID but the EMP_ID is always in descending order even when i changed the -1 to 1 and vice versa.. like this:


if(( ( HashMap ) object1 ).get ( "EMP_ID" ) == null) return 1;
if(( ( HashMap ) object2 ).get ( "EMP_ID" ) == null) return -1;


my result is always:

BLDG_ID   EMP_ID
333            999999
333            222222

222            888888

111            333333
111            111111


what i really want to see is:

BLDG_ID   EMP_ID
333            222222
333            999999

222             888888  


111            1111111
111            3333333


thank you so much for bearing with me..

0
 
ayeenAuthor Commented:
i guess to summarize,

my question are :

1. if my hashmap is already sorted in descending order and I call the compare routine how do i force it to stay in descending order (because right now it reverses into ascending)

2. if i have a secondary sort (in my case "EMP_ID"), how do I sort the BLDG_ID in descending order and the EMP_ID in ascending order? (because right now EMP_ID is always sorted in descending like the BLDG_ID even when i reverse the -1 to 1 and 1 to -1

thank you....
0
 
for_yanCommented:

these parts:
if(( ( HashMap ) object1 ).get ( "EMP_ID" ) == null) return 1;
if(( ( HashMap ) object2 ).get ( "EMP_ID" ) == null) return -1;

will affect only whether your items with null values as a whole will be before
items with non-null values or after items with non-null values
So if you witch 1 and -1 - this should only change position of non-null relative to null

In order to change asscending to desecending
you need to chnage this


  int first = obj1Value.compareTo ( obj2Value );

to this

  int first = obj2Value.compareTo ( obj1Value );

and this

return obj1Str.compareTo ( obj2Str );

to this:

return obj2Str.compareTo ( obj1Str );



0
 
for_yanCommented:
I guess you understaood form above - these parts with euqlity to null affect only nulls

We cannot forsce it to stay as its or anything like that but we can make sorting right no matter how it starts with

so look how it does itnow and reverese the part which you don't like

if you don't like about sortuing oder of buildings then
change this part:

  int first = obj1Value.compareTo ( obj2Value );
 to this:

  int first = obj2Value.compareTo ( obj1Value );

If you don't like the order of emp names
then change the other part:
this:
return obj1Str.compareTo ( obj2Str );

to this:

return obj2Str.compareTo ( obj1Str );

If you are unlappy wioth both orders - change
both parts

In any case the original order of the hahstables
should not matter









0
 
for_yanCommented:
I guess one case which we didn't cover is if they have both null buildings they will not be sorted by emp_name.

To do that yyou need to add one more piece:

(again the ultimate ascending/descending order try exeprimenting and inverting the way
I showed abaove):

class HashMapComp implements Comparator <HashMap<String, Object>> {

    public int compare (  HashMap<String, Object> object1 ,  HashMap<String, Object> object2 )
      {

if(( ( HashMap ) object1 ).get ( "BLDG_ID" ) == null) &&  ( HashMap ) object2 ).get ( "BLDG_ID" ) == null) ) {

if(( ( HashMap ) object1 ).get ( "EMP_ID" ) == null) return -1;
if(( ( HashMap ) object2 ).get ( "EMP_ID" ) == null) return 1;


    String obj1Str = ( Integer ) ( ( HashMap ) object1 ).get ( "EMP_ID" ) ;
              Stringr obj2Str = ( Integer ) ( ( HashMap ) object2 ).get ( "EMP_ID" ) ;

   return obj1Str.compareTo ( obj2Str );





}


if(( ( HashMap ) object1 ).get ( "BLDG_ID" ) == null) return 1;
if(( ( HashMap ) object2 ).get ( "BLDG_ID" ) == null) return -1;



              Integer obj1Value = ( Integer ) ( ( HashMap ) object1 ).get ( "BLDG_ID" ) ;
              Integer obj2Value = ( Integer ) ( ( HashMap ) object2 ).get ( "BLDG_ID" ) ;

             int first = obj1Value.compareTo ( obj2Value );

      if(first == 1) return 1;
   else if(first == -1) retirn -1;
  else
{

if(( ( HashMap ) object1 ).get ( "EMP_ID" ) == null) return -1;
if(( ( HashMap ) object2 ).get ( "EMP_ID" ) == null) return 1;


    String obj1Str = ( Integer ) ( ( HashMap ) object1 ).get ( "EMP_ID" ) ;
              Stringr obj2Str = ( Integer ) ( ( HashMap ) object2 ).get ( "EMP_ID" ) ;

   return obj1Str.compareTo ( obj2Str );


}

             return 0;
      }
  }

Open in new window

0
 
ayeenAuthor Commented:

thanks..

i tried the above changes but my EMP_ID is still in descending... i might have overlooked something...here's my whole code..


Collections.sort(theResults, new HashMapComparator());




public class HashMapComparator implements Comparator  <HashMap<String, Object>>{
      
    public int compare (  HashMap<String, Object> object1 ,  HashMap<String, Object> object2 )
      {
                               if(( ( HashMap ) object1 ).get ( "BLDG_ID" ) == null) return -1;
                  if(( ( HashMap ) object2 ).get ( "BLDG_ID" ) == null) return 1;

                 
                  String obj1Value = ( String ) ( ( HashMap ) object1 ).get ( "BLDG_ID" ) ;
                  String obj2Value = ( String ) ( ( HashMap ) object2 ).get ( "BLDG_ID" ) ;

                  int first =  obj1Value.compareTo ( obj2Value ) ;//has to be in descending order..

                  if(first == 1) return 1;
                  else if(first == -1) return -1;
                  else
                  {

                       if(( ( HashMap ) object1 ).get ( "EMP_ID" ) == null) return -1;
                       if(( ( HashMap ) object2 ).get ( "EMP_ID" ) == null) return 1;

                       String obj1Str = ( String ) ( ( HashMap ) object1 ).get ( "EMP_ID" ) ;
                       String obj2Str = ( String ) ( ( HashMap ) object2 ).get ( "EMP_ID" ) ;

                       return obj2Str.compareTo ( obj1Str );//has to be ascending..but still doing descending
                  }


                  //return 0;


      }
}
0
 
for_yanCommented:
and building also in descending ?
0
 
for_yanCommented:
This is the way you  wanted (see  code and output below):

your comparator was wrong  didn't even run -
these

String obj1Value = ( String ) ( ( HashMap ) object1 ).get ( "BLDG_ID" ) ;
                  String obj2Value = ( String ) ( ( HashMap ) object2 ).get ( "BLDG_ID" ) ;

I had to change to :

Integer obj1Value = ( Integer) ( ( HashMap ) object1 ).get ( "BLDG_ID" ) ;
                  Integer obj2Value = ( Integer ) ( ( HashMap ) object2 ).get ( "BLDG_ID" ) ;


That;s how it was before, by the way

Only in the second case you want String's wheen you comparte id's
but for buildings you have integers (look at your own original post):



import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Vector;

public class SortVectorOfHashMaps {

    public static void main(String[] args) {


Vector<HashMap<String, Object>> theVector= new Vector<HashMap<String, Object>>();


HashMap<String, Object> theHashMap= new HashMap<String, Object>();

//theHashMaphas these data:
//(pretend this is a for-loop)
//1st set<br>

theHashMap.put("BLDG_ID", 333);//int<br>
theHashMap.put("EMP_NAME", "AAA");//String<br>
theHashMap.put("FLAG", true);//boolean<br>
theVector.add(theHashMap);

//2nd set<br>
 theHashMap= new HashMap<String, Object>();

theHashMap.put("BLDG_ID", 222);//int<br>
theHashMap.put("EMP_NAME", "BBB");//String<br>
theHashMap.put("FLAG", false);//boolean<br>
//<br>
//<br>
theVector.add(theHashMap);
//<br>
theHashMap= new HashMap<String, Object>();
//2nd set<br>
theHashMap.put("BLDG_ID", 111);//int<br>
theHashMap.put("EMP_NAME", "CCC");//String<br>
theHashMap.put("FLAG", false);//boolean<br>



theVector.add(theHashMap);

  Collections.sort(theVector, new HashMapComparator());

        for(HashMap<String, Object> h : theVector){
            System.out.println(h);
        }

        
    }


}
  class HashMapComparator implements Comparator  <HashMap<String, Object>>{

    public int compare (  HashMap<String, Object> object1 ,  HashMap<String, Object> object2 )
      {
                               if(( ( HashMap ) object1 ).get ( "BLDG_ID" ) == null) return -1;
                  if(( ( HashMap ) object2 ).get ( "BLDG_ID" ) == null) return 1;


                  Integer obj1Value = ( Integer) ( ( HashMap ) object1 ).get ( "BLDG_ID" ) ;
                  Integer obj2Value = ( Integer ) ( ( HashMap ) object2 ).get ( "BLDG_ID" ) ;

                  int first =  obj1Value.compareTo ( obj2Value ) ;//has to be in descending order..

                  if(first == 1) return 1;
                  else if(first == -1) return -1;
                  else
                  {

                       if(( ( HashMap ) object1 ).get ( "EMP_ID" ) == null) return -1;
                       if(( ( HashMap ) object2 ).get ( "EMP_ID" ) == null) return 1;

                       String obj1Str = ( String ) ( ( HashMap ) object1 ).get ( "EMP_ID" ) ;
                       String obj2Str = ( String ) ( ( HashMap ) object2 ).get ( "EMP_ID" ) ;

                       return obj2Str.compareTo ( obj1Str );//has to be ascending..but still doing descending
                  }


                  //return 0;


      }
}
    

Open in new window


{EMP_NAME=CCC, BLDG_ID=111, FLAG=false}
{EMP_NAME=BBB, BLDG_ID=222, FLAG=false}
{EMP_NAME=AAA, BLDG_ID=333, FLAG=true}

Open in new window


0
 
for_yanCommented:
So this seesm to do everything we wanted
(building ascending, emp_id descendning, even nulls
are treated as expected - after non-nulls)
See output below:

import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Vector;

public class SortVectorOfHashMaps {

    public static void main(String[] args) {


Vector<HashMap<String, Object>> theVector= new Vector<HashMap<String, Object>>();


HashMap<String, Object> theHashMap= new HashMap<String, Object>();

//theHashMaphas these data:
//(pretend this is a for-loop)
//1st set<br>

theHashMap.put("BLDG_ID", 333);//int<br>
theHashMap.put("EMP_ID", "AAA");//String<br>
theHashMap.put("FLAG", true);//boolean<br>
theVector.add(theHashMap);

//2nd set<br>
 theHashMap= new HashMap<String, Object>();

theHashMap.put("BLDG_ID", 222);//int<br>
theHashMap.put("EMP_ID", "BBB");//String<br>
theHashMap.put("FLAG", false);//boolean<br>
//<br>
//<br>
theVector.add(theHashMap);
//<br>
theHashMap= new HashMap<String, Object>();
//2nd set<br>
theHashMap.put("BLDG_ID", 111);//int<br>
theHashMap.put("EMP_ID", "CCC");//String<br>
theHashMap.put("FLAG", false);//boolean<br>



theVector.add(theHashMap);


theHashMap= new HashMap<String, Object>();
//2nd set<br>
theHashMap.put("BLDG_ID", 111);//int<br>
theHashMap.put("EMP_ID", "UUU");//String<br>
theHashMap.put("FLAG", false);//boolean<br>



theVector.add(theHashMap);


theHashMap= new HashMap<String, Object>();
//2nd set<br>
theHashMap.put("BLDG_ID", 111);//int<br>
theHashMap.put("EMP_ID", "XXX");//String<br>
theHashMap.put("FLAG", false);//boolean<br>



theVector.add(theHashMap);


theHashMap= new HashMap<String, Object>();
//2nd set<br>
//theHashMap.put("BLDG_ID", 111);//int<br>
theHashMap.put("EMP_ID", "CCCAAA");//String<br>
theHashMap.put("FLAG", false);//boolean<br>

theVector.add(theHashMap);


theHashMap= new HashMap<String, Object>();
//2nd set<br>
//theHashMap.put("BLDG_ID", 111);//int<br>
theHashMap.put("EMP_ID", "ZZZCCCAAA");//String<br>
theHashMap.put("FLAG", false);//boolean<br>

theVector.add(theHashMap);


theHashMap= new HashMap<String, Object>();
//2nd set<br>
//theHashMap.put("BLDG_ID", 111);//int<br>
//theHashMap.put("EMP_ID", "ZZZCCCAAA");//String<br>
theHashMap.put("FLAG", false);//boolean<br>

theVector.add(theHashMap);









  Collections.sort(theVector, new HashMapComparator());

        for(HashMap<String, Object> h : theVector){
            System.out.println(h);
        }

        
    }


}
  /*
class HashMapComp implements Comparator <HashMap<String, Object>> {

    public int compare (  HashMap<String, Object> object1 ,  HashMap<String, Object> object2 )
      {

              Integer obj1Value = ( Integer ) ( ( HashMap ) object1 ).get ( "BLDG_ID" ) ;
              Integer obj2Value = ( Integer ) ( ( HashMap ) object2 ).get ( "BLDG_ID" ) ;

              return obj1Value.compareTo ( obj2Value ) ;

      }
  }

*/
 class HashMapComparator implements Comparator  <HashMap<String, Object>>{

    public int compare (  HashMap<String, Object> object1 ,  HashMap<String, Object> object2 )
      {

if(((( HashMap ) object1 ).get ( "BLDG_ID" ) == null) &&  ((( HashMap ) object2 ).get ( "BLDG_ID" ) == null) ) {

if(( ( HashMap ) object1 ).get ( "EMP_ID" ) == null) return 1;
if(( ( HashMap ) object2 ).get ( "EMP_ID" ) == null) return -1;


    String obj1Str = ( String) ( ( HashMap ) object1 ).get ( "EMP_ID" ) ;
              String obj2Str = ( String ) ( ( HashMap ) object2 ).get ( "EMP_ID" ) ;

   return obj2Str.compareTo ( obj1Str );





}


                               if(( ( HashMap ) object1 ).get ( "BLDG_ID" ) == null) return 1;
                  if(( ( HashMap ) object2 ).get ( "BLDG_ID" ) == null) return -1;


                  Integer obj1Value = ( Integer) ( ( HashMap ) object1 ).get ( "BLDG_ID" ) ;
                  Integer obj2Value = ( Integer ) ( ( HashMap ) object2 ).get ( "BLDG_ID" ) ;

                  int first =  obj1Value.compareTo ( obj2Value ) ;//has to be in descending order..

                  if(first == 1) return 1;
                  else if(first == -1) return -1;
                  else
                  {

                       if(( ( HashMap ) object1 ).get ( "EMP_ID" ) == null) return -1;
                       if(( ( HashMap ) object2 ).get ( "EMP_ID" ) == null) return 1;

                       String obj1Str = ( String ) ( ( HashMap ) object1 ).get ( "EMP_ID" ) ;
                       String obj2Str = ( String ) ( ( HashMap ) object2 ).get ( "EMP_ID" ) ;

                       return obj2Str.compareTo ( obj1Str );//has to be ascending..but still doing descending
                  }


                  //return 0;


      }
}
    

Open in new window


Output:
{BLDG_ID=111, EMP_ID=XXX, FLAG=false}
{BLDG_ID=111, EMP_ID=UUU, FLAG=false}
{BLDG_ID=111, EMP_ID=CCC, FLAG=false}
{BLDG_ID=222, EMP_ID=BBB, FLAG=false}
{BLDG_ID=333, EMP_ID=AAA, FLAG=true}
{EMP_ID=ZZZCCCAAA, FLAG=false}
{EMP_ID=CCCAAA, FLAG=false}
{FLAG=false}

Open in new window

0
 
ayeenAuthor Commented:
i will try this. boss decided theres more impt task he needs to make me do, lol...i'll be back!
0
 
ayeenAuthor Commented:
thanks again for_yan..

sorry about the confusion with my data type for BLDG_ID, the real live data was actually an integer i just didn't bother to correct it here so as not to create more confusion...

but anyway, this worked great! i just had to change the following:

(1)

       Integer obj1Value = ( Integer) ( ( HashMap ) object1 ).get ( "BLDG_ID" ) ;
       Integer obj2Value = ( Integer ) ( ( HashMap ) object2 ).get ( "BLDG_ID" ) ;

       int first =  obj2Value.compareTo ( obj1Value ) ;

for my BLDG_ID to sort in descending order

and

(2)
            String obj1Str = ( String ) ( ( HashMap ) object1 ).get ( "EMP_ID" ) ;
            String obj2Str = ( String ) ( ( HashMap ) object2 ).get ( "EMP_ID" ) ;

            return obj1Str.compareTo ( obj2Str );


for my EMP_ID to sort in ascending order


right now i'm experimenting on adding more levels of sorting....


for the sake of other viewers of this thread, this is how i added my third level sort


public int compare(  HashMap<String, Object> object1 ,  HashMap<String, Object> object2 )
{
int second = 0;
// copy above codes from for_ryan for the BLDG_ID then on the part that has:

String obj1Str = ( String ) ( ( HashMap ) object1 ).get ( "EMP_ID" ) ;
String obj2Str = ( String ) ( ( HashMap ) object2 ).get ( "EMP_ID" ) ;

// change return obj2Str.compareTo ( obj1Str ); to the code below

second =  obj2Str.compareTo ( obj1Str );//descending order


//then add this third level sort
       if(second == 1) return 1;
       else if(second == -1) return -1;
       else
       {
            if(( ( HashMap ) object1 ).get ( "SUBORDINATE_ID" ) == null) return -1;
            if(( ( HashMap ) object2 ).get ( "SUBORDINATE_ID" ) == null) return 1;
        
            Integer obj1Str = ( Integer ) ( ( HashMap ) object1 ).get ( "SUBORDINATE_ID" ) ;
            Integer obj2Str = ( Integer ) ( ( HashMap ) object2 ).get ( "SUBORDINATE_ID" ) ;

            return obj1Str.compareTo ( obj2Str );// (ascending)
       }

Open in new window

0
 
ayeenAuthor Commented:
I've requested that this question be closed as follows:

Accepted answer: 500 points for for_yan's comment http:/Q_27407371.html#37009798
Assisted answer: 0 points for ayeen's comment http:/Q_27407371.html#37031532

for the following reason:

thank you!
0
 
ayeenAuthor Commented:
i need to award some points to CEHJ too
0
 
CEHJCommented:
:)
0
 
ayeenAuthor Commented:
it worked.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 15
  • 15
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now