?
Solved

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

Posted on 2006-04-27
30
Medium Priority
?
345 Views
Last Modified: 2010-05-18
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
Comment
Question by:ryno71
  • 15
  • 10
  • 4
  • +1
30 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 16558163
Get the result from the one first, and if equal, press on to the next. Is it java.util.Date?
0
 
LVL 92

Expert Comment

by:objects
ID: 16558174
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
 

Author Comment

by:ryno71
ID: 16558194
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:ryno71
ID: 16558206
The phone number is a 10 digit numeric.
0
 

Author Comment

by:ryno71
ID: 16558221
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 16558222
>>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
 
LVL 92

Assisted Solution

by:objects
objects earned 800 total points
ID: 16558240
> 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
 

Author Comment

by:ryno71
ID: 16558258
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
 

Author Comment

by:ryno71
ID: 16558263
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 16558264
>>Sorry I want to sort the objects  by date then by phone number

You'd do it like i mentioned above
0
 
LVL 92

Expert Comment

by:objects
ID: 16558269
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
 
LVL 2

Expert Comment

by:gauravkrtomar
ID: 16561588
>>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
 

Author Comment

by:ryno71
ID: 16569477
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
 
LVL 92

Expert Comment

by:objects
ID: 16570536
i'd just use a Comparator
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16572044
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
 

Author Comment

by:ryno71
ID: 16572951
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
 

Author Comment

by:ryno71
ID: 16572961
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
 

Author Comment

by:ryno71
ID: 16573006
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 16573186
This sounds odd to me. Social security numbers shouldn't be involved in ranking and the same goes for phone number
0
 

Author Comment

by:ryno71
ID: 16573242
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 16573254
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
 

Author Comment

by:ryno71
ID: 16573332
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 16573441
There wouldn't be four times. What actually is rankable - date?
0
 

Author Comment

by:ryno71
ID: 16573643
first the customer identifier (8 characters), then date (20 characters), then the third number (5 digit numeric)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16576478
>> 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
 

Author Comment

by:ryno71
ID: 16576721
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
 
LVL 86

Accepted Solution

by:
CEHJ earned 1200 total points
ID: 16576821
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
 

Author Comment

by:ryno71
ID: 16576827
Sorry for the confusion earlier... when I'm at work on the weekends takes a little longer to get things done...
0
 

Author Comment

by:ryno71
ID: 16577090
Oh and thanks guys....
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16577551
:-)
0

Featured Post

Industry Leaders: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses
Course of the Month16 days, 13 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question