Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #10657 > unrolled thread

Comparator?

Started byKnute Johnson <nospam@knutejohnson.com>
First post2011-12-11 17:26 -0800
Last post2011-12-12 07:28 -0800
Articles 5 — 3 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  Comparator? Knute Johnson <nospam@knutejohnson.com> - 2011-12-11 17:26 -0800
    Re: Comparator? Patricia Shanahan <pats@acm.org> - 2011-12-11 18:16 -0800
      Re: Comparator? Knute Johnson <nospam@knutejohnson.com> - 2011-12-12 07:27 -0800
    Re: Comparator? Roedy Green <see_website@mindprod.com.invalid> - 2011-12-12 01:05 -0800
      Re: Comparator? Knute Johnson <nospam@knutejohnson.com> - 2011-12-12 07:28 -0800

#10657 — Comparator?

FromKnute Johnson <nospam@knutejohnson.com>
Date2011-12-11 17:26 -0800
SubjectComparator?
Message-ID<jc3l8r$rj6$1@dont-email.me>
I've got a question about how to create a Comparator that I can use to 
keep a sorted list of objects.  The sort fields would be a code that is 
not something to be sorted directly and a String that I would want 
alphabetical.  I would have a list of the codes and that list would be 
in the sorted order.  What I want to do is put the data in order 
according to the list of codes and the other String.

The codes are actually going to be a 3 letter String.  For example the 
codes could be;

XAB
AJC
BQP
RST

and the data would be a code String and another String to sort 
alphabetically.

XAB San Francisco
BQP Alabama
XAB Houston
AJC Montreal

I want the data sorted in this order:

XAB Houston
XAB San Francisco
AJC Montreal
BQP Alabama

Any simple ideas on how to design the Comparator?

Thanks,

-- 

Knute Johnson

[toc] | [next] | [standalone]


#10658

FromPatricia Shanahan <pats@acm.org>
Date2011-12-11 18:16 -0800
Message-ID<8NGdnTnFTOyd-XjTnZ2dnUVZ_uGdnZ2d@earthlink.com>
In reply to#10657
Knute Johnson wrote:
> I've got a question about how to create a Comparator that I can use to 
> keep a sorted list of objects.  The sort fields would be a code that is 
> not something to be sorted directly and a String that I would want 
> alphabetical.  I would have a list of the codes and that list would be 
> in the sorted order.  What I want to do is put the data in order 
> according to the list of codes and the other String.
> 
> The codes are actually going to be a 3 letter String.  For example the 
> codes could be;
> 
> XAB
> AJC
> BQP
> RST
> 
> and the data would be a code String and another String to sort 
> alphabetically.
> 
> XAB San Francisco
> BQP Alabama
> XAB Houston
> AJC Montreal
> 
> I want the data sorted in this order:
> 
> XAB Houston
> XAB San Francisco
> AJC Montreal
> BQP Alabama
> 
> Any simple ideas on how to design the Comparator?
> 
> Thanks,
> 

I would deal with the codes by creating a Map<String,Integer>, and
initializing it to map each code to its index in the original list.

In the compare method, first compare the results of looking up the codes
in the map. If that gives equality, then compare the city name strings
to resolve the comparison.

Patricia

[toc] | [prev] | [next] | [standalone]


#10672

FromKnute Johnson <nospam@knutejohnson.com>
Date2011-12-12 07:27 -0800
Message-ID<jc56hq$516$1@dont-email.me>
In reply to#10658
On 12/11/2011 6:16 PM, Patricia Shanahan wrote:
> Knute Johnson wrote:
>> I've got a question about how to create a Comparator that I can use to
>> keep a sorted list of objects. The sort fields would be a code that is
>> not something to be sorted directly and a String that I would want
>> alphabetical. I would have a list of the codes and that list would be
>> in the sorted order. What I want to do is put the data in order
>> according to the list of codes and the other String.
>>
>> The codes are actually going to be a 3 letter String. For example the
>> codes could be;
>>
>> XAB
>> AJC
>> BQP
>> RST
>>
>> and the data would be a code String and another String to sort
>> alphabetically.
>>
>> XAB San Francisco
>> BQP Alabama
>> XAB Houston
>> AJC Montreal
>>
>> I want the data sorted in this order:
>>
>> XAB Houston
>> XAB San Francisco
>> AJC Montreal
>> BQP Alabama
>>
>> Any simple ideas on how to design the Comparator?
>>
>> Thanks,
>>
>
> I would deal with the codes by creating a Map<String,Integer>, and
> initializing it to map each code to its index in the original list.
>
> In the compare method, first compare the results of looking up the codes
> in the map. If that gives equality, then compare the city name strings
> to resolve the comparison.
>
> Patricia

Thanks very much Patricia.

-- 

Knute Johnson

[toc] | [prev] | [next] | [standalone]


#10659

FromRoedy Green <see_website@mindprod.com.invalid>
Date2011-12-12 01:05 -0800
Message-ID<qqgbe719blhm4342gksgnti30pu7gr4f5e@4ax.com>
In reply to#10657
On Sun, 11 Dec 2011 17:26:51 -0800, Knute Johnson
<nospam@knutejohnson.com> wrote, quoted or indirectly quoted someone
who said :

>I've got a question about how to create a Comparator that I can use to 
>keep a sorted list of objects

see http://mindprod.com/applet/comparatorcutter.html

Just fill in the blanks and it will generate you the Java code you
need.

You could do thing with strings with the airport code in the first
three slots or with objects with an airport and City name String
field.
-- 
Roedy Green Canadian Mind Products
http://mindprod.com
For me, the appeal of computer programming is that
even though I am quite a klutz,
I can still produce something, in a sense
perfect, because the computer gives me as many
chances as I please to get it right.
 

[toc] | [prev] | [next] | [standalone]


#10673

FromKnute Johnson <nospam@knutejohnson.com>
Date2011-12-12 07:28 -0800
Message-ID<jc56i8$516$2@dont-email.me>
In reply to#10659
On 12/12/2011 1:05 AM, Roedy Green wrote:
> On Sun, 11 Dec 2011 17:26:51 -0800, Knute Johnson
> <nospam@knutejohnson.com>  wrote, quoted or indirectly quoted someone
> who said :
>
>> I've got a question about how to create a Comparator that I can use to
>> keep a sorted list of objects
>
> see http://mindprod.com/applet/comparatorcutter.html
>
> Just fill in the blanks and it will generate you the Java code you
> need.
>
> You could do thing with strings with the airport code in the first
> three slots or with objects with an airport and City name String
> field.

Thanks Roedy, I'll take a look.

-- 

Knute Johnson

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web