Path: csiph.com!x330-a1.tempe.blueboxinc.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: Mon, 26 Sep 2011 10:04:19 -0500 Date: Mon, 26 Sep 2011 08:04:13 -0700 From: Patricia Shanahan User-Agent: Mozilla/5.0 (Windows NT 5.2; WOW64; rv:6.0.2) Gecko/20110902 Thunderbird/6.0.2 MIME-Version: 1.0 Newsgroups: comp.lang.java.help Subject: Re: How to sort String array A based on int array B References: <1k2n77ltdv7ml661pa9a2g63j8q5l1c66g@4ax.com> <822p77tjmlsdrp2r7so1ko70igu9fascn6@4ax.com> <5PmdnbXtmIHNYOHTnZ2dnUVZ_vCdnZ2d@earthlink.com> In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Message-ID: Lines: 55 X-Usenet-Provider: http://www.giganews.com NNTP-Posting-Host: 70.230.204.200 X-Trace: sv3-NkmqkWkq7ZGg6tK29loQ/CNqF2RnEqDQW25UQ9dYUdAHKyBGwgB/wi8Nya1XMBLFxzaLyX5nBizvNC3!6472AMfdIIMj5UesxJO//dKIRUJSaK28CKOQwrYaH/LaJi7X1lGSYeApLvWLcBj+SBwbzuI/3iGb!BZwALOcXUaOwU8UdAmkcrC8K6FFyDgtbZ9kcPROrrcjr7Fg= 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: 3857 Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.help:1134 On 9/26/2011 6:37 AM, Thee Chicago Wolf (MVP) wrote: >> On 9/23/2011 6:27 PM, Thee Chicago Wolf (MVP) wrote: >> ... >>> The int array is already sorted: >>> >>> int[] rank = {1,2,3,4,5,6}; >>> >>> The song tiltles are not sorted: >>> >>> String[] song = {"6_song", >>> "4_song", >>> "1_song", >>> "3_song", >>> "2_song", >>> "5_song"}; >>> >>> I took the top 6 songs from the "Top 40 Charts" that are already >>> ranked from 1-6. But is there anything in the original problem statement that justifies requiring pre-sorted data? If you only have to sort according to Top 40 rank, and you have the Top 40 list, you can ignore A and simply return the input data without doing anything to it at all. I don't think that is what the problem statement meant, because sort algorithms would be irrelevant and useless for that problem. >> >> This does not have much to do with the requirement you quoted earlier. > > Patricia, what I think you're failing to see is that to rank a bunch > of songs based on a *defined* rank of most to least popular will just > re-randomize them and not order them in a rank. Top 40 songs are not > ordered randomly but by popularity so you already *know* their rank as > a byproduct of their popularity. Here's an example that may make my interpretation of the original problem statement a bit clearer. Suppose B contains {"xxx", "yyy", "zzz"} and A contains {3, 1, 2}. Then the rank ordered version of B would be {"yyy", "zzz", "xxx"}. That is, the song that, according to A, has rank 1 comes first, then the song that has rank 2, and finally the song with rank 3. In general, when given a sorting problem, it does not make sense to assume the input data is already sorted according to the required sort order. It may be sorted according to some other criterion, such as alphabetical order, or it may be unsorted. For example, someone somewhere started with a database containing popular songs in some arbitrary order, such as alphabetical by title or in order of appearance. They also had a table containing the number of sales of each song. They built the Top 40 list by sorting in rank order and truncating at 40. Patricia