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


Groups > comp.lang.java.programmer > #10672

Re: Comparator?

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!eternal-september.org!feeder.eternal-september.org!mx04.eternal-september.org!.POSTED!not-for-mail
From Knute Johnson <nospam@knutejohnson.com>
Newsgroups comp.lang.java.programmer
Subject Re: Comparator?
Date Mon, 12 Dec 2011 07:27:54 -0800
Organization A noiseless patient Spider
Lines 51
Message-ID <jc56hq$516$1@dont-email.me> (permalink)
References <jc3l8r$rj6$1@dont-email.me> <8NGdnTnFTOyd-XjTnZ2dnUVZ_uGdnZ2d@earthlink.com>
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
Injection-Date Mon, 12 Dec 2011 15:27:55 +0000 (UTC)
Injection-Info mx04.eternal-september.org; posting-host="mz/LDSJwiWnk3Jnnqg7x+Q"; logging-data="5158"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX194uBKhnaHAo7zDyRaZJFSY"
User-Agent Mozilla/5.0 (Windows NT 5.1; rv:8.0) Gecko/20111105 Thunderbird/8.0
In-Reply-To <8NGdnTnFTOyd-XjTnZ2dnUVZ_uGdnZ2d@earthlink.com>
Cancel-Lock sha1:6sj0t6UgG6GmvgOwox334qDE4W8=
Xref x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:10672

Show key headers only | View raw


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

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web