Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!border3.nntp.dca.giganews.com!Xl.tags.giganews.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local2.nntp.dca.giganews.com!nntp.earthlink.com!news.earthlink.com.POSTED!not-for-mail NNTP-Posting-Date: Sun, 11 Dec 2011 20:16:32 -0600 Date: Sun, 11 Dec 2011 18:16:31 -0800 From: Patricia Shanahan User-Agent: Thunderbird 1.5.0.2 (Windows/20060308) MIME-Version: 1.0 Newsgroups: comp.lang.java.programmer Subject: Re: Comparator? References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Message-ID: <8NGdnTnFTOyd-XjTnZ2dnUVZ_uGdnZ2d@earthlink.com> Lines: 44 X-Usenet-Provider: http://www.giganews.com NNTP-Posting-Host: 70.230.194.31 X-Trace: sv3-UDSnaIYQlYJb+0GTK2qCgIO6eTESNR0d85FVSxgU1FuUM1NLkLSFMFL90xOw7AwH912dV/chNgQFY0d!3/cCWdwSp7MSrjj+0rtNARFr6gaV3y8enA5JttFY9S2qTLVkA6yGZyWZYNMMv+YtAXe3C4+TE6jV!+84CVewaS0XdmJbvmd88jQxpBBYQVVa84oStLYFT42s8hg== X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.40 X-Original-Bytes: 2346 Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:10658 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, 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