Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!news2.arglkargh.de!news.litech.org!news.glorb.com!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: Fri, 23 Sep 2011 14:40:01 -0500 Date: Fri, 23 Sep 2011 12:39:58 -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> In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Message-ID: Lines: 64 X-Usenet-Provider: http://www.giganews.com NNTP-Posting-Host: 70.230.204.200 X-Trace: sv3-cD5YprokzqFUfnPZn6KzmUs7sTHqJHuMUSpuhNe/Dqd2xGadi46/f7QXrt25OyH09EOW9Sn/vEwmNIV!TL0PFs0CnIslwwBlfPK4fPUMWZMJX6UucMHM+sNHaiiMUMPgdXHI0HvMZwErge6qjUwCnt6jd21V!SA7Cujh9bCKEM67KtcLEIRQhrQPWvSXnNgIuub4CSc9D5IE= 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: 4273 Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.help:1120 On 9/23/2011 12:31 PM, Thee Chicago Wolf [MVP] wrote: >> On 9/23/2011 8:51 AM, Thee Chicago Wolf [MVP] wrote: ... >>> Here is the original requirement: >>> >>> Modify any of the sort algorithms to sort a secondary array B based on >>> the sorting order of A (that is akin to say: sort the titles of the >>> songs (B) based on their rank (A). >>> >>> I chose to add Artist info in addition to song title. This is why I >>> specifically added the #_ (read as number underscore) to the String >>> array else I figured there was no easy way to sort them based on a >>> rank. Meaning, you can't compare an int to a String. In my mind, I >>> thought that adding the #_ to the beginning of the String item would >>> make it easier to sort it. Does that make sense? >> >> Generally, adding your own features to a program before doing what is >> actually required is a very bad mistake. First get what is needed >> working, and have that version available to turn in. If you have time >> later, and want to get some more programming practice, add your own >> features. >> >> The way I would interpret the original requirement is that B contains >> the song titles, and absolutely nothing but the song titles. No numbers >> - unless they are part of the song title, no underscore, no artist >> information. A[i] is the rank of B[i]. That is, if B[i] is the highest >> rated title, A[i] is 1. If B[i] is the lowest rated of six titles then >> A[i] is 6. >> >> In this interpretation there is no need to compare an int to a String, >> only to compare an int to another int. > > Maybe I am missing the gist here but I would still be comparing an int > to a String, not int to int. B[i] will be a bunch of song titles that > would be of type String. You're not saying compare index to index, > right? If my suggestion is the correct interpretation of the problem, you only ever need to compare an element of A to another element of A, and all elements of A are integers. The only strings you have are song titles. A song title tells you nothing about its rank, so no comparison involving a string can possibly be relevant to the sort order. > >> I'll give you a couple of hints: >> >> 1. Maintain the relationship between A and B as an invariant during the >> sort. >> >> 2. At the end of the sort, A will be in ascending value order if you >> want highest rank, lowest rank number, title first. > > Nothing in A[] should be changing as those values are static, only the > order of the titles in B[] should be sorted based on the defined order > of what's in A[]. If you are, in fact, required to keep A unchanged I strongly recommend making a modifiable copy of it at the start, and maintain the relationship between B and the modifiable copy of A. Patricia