• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 349
  • Last Modified:

Sorting two objects by two different fields (by date then another number)

What would be the best way to sort two objects by date and then by another number say phone number?

Boths Objects have a date field, a text field, and an another number say a phone number.  I would like to sort by the data first and then by the phone number.  I should use a treelist/comparator.  


 I've been looking for examples or good info on sorting twice.  any ideas of where to look?  Or examples?

thanks
ryno71
0
ryno71
Asked:
ryno71
  • 15
  • 10
  • 4
  • +1
2 Solutions
 
CEHJCommented:
Get the result from the one first, and if equal, press on to the next. Is it java.util.Date?
0
 
objectsCommented:
Use different Comparators, one to sort by phone number and the other to sort by date

or in a table you can use a column sorter

http://javaalmanac.com/egs/javax.swing.table/Sorter.html
0
 
ryno71Author Commented:
Actually CEHJ
the object has the date (year, month, day, hour, minute,second) with a length of 14 characters.

But yes sort by this date oldest to newest and then by the phone number.

Thanks
ryno71
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
ryno71Author Commented:
The phone number is a 10 digit numeric.
0
 
ryno71Author Commented:
If I ran them through two Comparators would the second one rearrange them by the phone number then and not keep the date ordering?
0
 
CEHJCommented:
>>the object has the date (year, month, day, hour, minute,second) with a length of 14 characters.

So it's a String. Use a DateFormat to parse it and then you can call compareTo on it
0
 
objectsCommented:
> If I ran them through two Comparators would the second one rearrange them by the phone number then and not keep the date ordering?

I thought you wanted to have two different sort orders.
If you wanrt to use both fields in a single sort then have your comparator do something like this:

if (date1.equals(date2))
{
   return phone1.compareTo(phone2);
}
else
{
   return date1.compareTo(date2);
}

0
 
ryno71Author Commented:
Sorry I want to sort the objects  by date then by phone number, so the returned objects would be sorted by date for the first sort (oldest to newest) and then by the phone number

 
0
 
ryno71Author Commented:
keeping the first sort (date) as the primary sort and using the phone number as a secondary sort
ryno71

Sorry I'm kinda lupey right now... coffee isn't helping
0
 
CEHJCommented:
>>Sorry I want to sort the objects  by date then by phone number

You'd do it like i mentioned above
0
 
objectsCommented:
what I posted above would give the following order, is this what u need:

1/1/06 123
1/1/06 456
2/2/06 145
2/2/06 222
0
 
gauravkrtomarCommented:
>>Comment from CEHJ
>>Date: 04/28/2006 04:38AM IST
>>      Comment       

>>Get the result from the one first, and if equal, press on to the next.
I think what CEHJ is saying is correct you need two comparators if you want to order them by two ways i.e. at like if you click on the Date column sort it by Date if clicked on another number column sort it by another number. But if you just want that the objects shoudl be sorted by Date and then Number then u can use the single comparator in following manner:-
compareTo(Object obj){
  if(date of this and date of Obj are equal){
    return compare(((CustomObject)obj).anothernumber,anothernumber);
  }else{
    return this.date.compareTo(((CustomObject)obj).date);
  }
}

You only need to sort on the basis of number only if the dates are same otherwise the date will be deciding for the sorting.
0
 
ryno71Author Commented:
Just got more complex..  there is another 2 items I need to sort by that are also in the object...  social security number and age
So social security number then date then phone number then age .. the object also has a file name inside it.... the good thing is when I've sorted everything I can just hand off the filename (filehandle of sorts)

So in this case I think I should create a treemap?

Placing a concatenated string with social security.date.phonenumber.age as the key and filename as the value

would you guys agree?

and if was to place these items in a treemap as such what would be the easiest way to compare keys?  Say if its sorted as such already

1234567890.20060909092322.7075551212.32
1234567890.20060909092322.7075551212.32  <===== duplicate
1345666666.20060909092322.7085551212

I'd say I have to use a StringTokenizer to parse thru the concatenated string after they were sorted.  If I was only really interested in comparing the phonenumber if it was a duplicate remove it (map.remove)


if the age was a ceratin number add I wanted to add this to the one above (both together in the treemap) would I need to create a new map at this point or could I just do a remove and then a put?

If I am thinking about this the wrong way please feel free to tell me!

thanks
ryno71

0
 
objectsCommented:
i'd just use a Comparator
0
 
CEHJCommented:
You can't use the concatenation idea to aid in the sorting unfortunately, if that's what you're angling at. This is because some field are numeric. In this case, it's a question of just chaining through all the required fields in the Comparator
0
 
ryno71Author Commented:
Trying to make sure I am understand both of you

class myobject implements Comparable{

private String socialsecurity;
private String Date;
private int phonenumber;
private int age;

public String getSocial(String social)
{

}
0
 
ryno71Author Commented:
oops sorry for the duplicate post

Trying to make sure I am understand both of you

class myobject implements Comparable{

private String socialsecurity;
private String Date;
private int phonenumber;
private int age;

public String getSocial()
{
   return socialsecurity;
}

public void setSocial(String social)
{
  this.socialsecurity-social
}

rest of gettter and setters


public in compareTo(Object a, Object b)
{


}

0
 
ryno71Author Commented:
oops sorry for the duplicate posts

Trying to make sure I am understand both of you

class myobject implements Comparable{

private String socialsecurity;
private String Date;
private int phonenumber;
private int age;

public String getSocial()
{
   return socialsecurity;
}

public void setSocial(String social)
{
  this.socialsecurity-social
}

rest of gettter and setters


public in compareTo(Object a, Object b)
{
    String social1,social2;
    asocial =(()a.getSocial();
    bsocial=(()b.Social();
    return asocial.compareTo(bsocial);  //sort by social

}



or should I try to make a bunch of Comparators?

public static Comparator SocialSecurity = new Comparator()
{
      public in compare(Object sociala, Object socialb)
      String asocial=((myobject)sociala).getsocial();
            String bsocial=((myobject)socialb).getsocial();

      if (!(asocial.equals(bsocial)))
                 return asocial.comparesTo(bsocial);
            else
                   return some other way of sorting?
}

ryno71
0
 
CEHJCommented:
This sounds odd to me. Social security numbers shouldn't be involved in ranking and the same goes for phone number
0
 
ryno71Author Commented:
basically the social secuirty number was the first thing I could think of it regards to a unique customer type of identifier... NOT doing a search by a social security number I was just generalizing...  

ryno71
0
 
CEHJCommented:
Well i think you may be conflating two things here - implementation of equals and implementation of Comparable. A SSN is a perfect candidate for equals, but would not be part of Comparable
0
 
ryno71Author Commented:
So I should use a  Comparator still?  Structurally how shoul dit work to sort by the four times...  just trying to visuallize how to go about it...

ryno71
0
 
CEHJCommented:
There wouldn't be four times. What actually is rankable - date?
0
 
ryno71Author Commented:
first the customer identifier (8 characters), then date (20 characters), then the third number (5 digit numeric)
0
 
CEHJCommented:
>> first the customer identifier (8 characters), then date (20 characters), then the third number (5 digit numeric)


I'm getting confused now - these were your fields previously:

>>
private String socialsecurity;
private String Date;
private int phonenumber;
private int age;
>>

so what are the first and last ones in my first quotation referring to?
0
 
ryno71Author Commented:
I had wanted to sort for 4 items in 2 objects originally... yes

It was String,String, int, and another int.   Doesn't really matter if I sort for 3 or 4.. just want to figure out how to set it up to search for more than 2

0
 
CEHJCommented:
More than 2 is the same principle as more than one. Here it is for String-Date-int


public int compareTo(Object o) {
      myobject other = (myobject)o;
      int result = stringField.compareTo(other.stringField);
      if (result == 0) {
            result = dateField.compareTo(other.dateField);
            if (result == 0) {
                  result = intField - other.intField;
            }
      }
      return result;
}
0
 
ryno71Author Commented:
Sorry for the confusion earlier... when I'm at work on the weekends takes a little longer to get things done...
0
 
ryno71Author Commented:
Oh and thanks guys....
0
 
CEHJCommented:
:-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

  • 15
  • 10
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now