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: Fri, 23 Sep 2011 22:30:56 -0500 From: "Charles Hottel" Newsgroups: comp.lang.java.help References: <1k2n77ltdv7ml661pa9a2g63j8q5l1c66g@4ax.com> <822p77tjmlsdrp2r7so1ko70igu9fascn6@4ax.com> <5PmdnbXtmIHNYOHTnZ2dnUVZ_vCdnZ2d@earthlink.com> Subject: Re: How to sort String array A based on int array B Date: Fri, 23 Sep 2011 23:31:01 -0400 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2900.5931 X-RFC2646: Format=Flowed; Original X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.6109 Message-ID: Lines: 130 X-Usenet-Provider: http://www.giganews.com NNTP-Posting-Host: 141.156.48.200 X-Trace: sv3-v9y7aqxzB2I9UKu3pG6347O+Gq7Mnk9hwY3zzKSQTJrePuwvXShcobemVtWks4b2p3NFq8JYS0+WWBg!dC28NcZqERi9yiEVk2u4Vqx/xFBhdfDN8GErdl+jFGklpv/84xQXQfXinnYQJ8Zr0q+8AwlIvnvA!sttoMD6eKUykPmOe7FO7MTC6bgjkYjnW8Q== 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: 6810 Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.help:1128 "Thee Chicago Wolf (MVP)" <.@.> wrote in message news:m8cq77pb4h2odihba17kjmdeb1hkhmo115@4ax.com... > >"Thee Chicago Wolf [MVP]" <.@.> wrote in message >>news:j5np771d6gjjdo1m1cj43fo4e6s3lknb1f@4ax.com... >>> >On 9/23/2011 8:51 AM, Thee Chicago Wolf [MVP] wrote: >>>>>> On 9/23/2011 6:51 AM, Thee Chicago Wolf [MVP] wrote: >>>>>>>> On 9/22/2011 12:31 PM, Thee Chicago Wolf [MVP] wrote: >>>>>>>> >>>>>>>>> The trouble I am having is sorting array B based on array A. >>>>>>>> >>>>>>>> >>>>>>>> I don't doubt that. I'm having trouble understanding what "based >>>>>>>> on" >>>>>>>> means here. Can you elaborate? What actual operation do you want >>>>>>>> to >>>>>>>> preform on A and B, based on what criteria? >>>>>>> >>>>>>> Ok, after reading my post I can see that I was not quite as clear as >>>>>>> I >>>>>>> could have been. >>>>>>> >>>>>>> So I have two arrays: A and B. Array A is an int array (I think I >>>>>>> had >>>>>>> it backwards in my original description) and array B is a String >>>>>>> array. Array A is already ordered from 1 - 6 in ascending order and >>>>>>> is >>>>>>> basically a rank order with 1 being most popular, 2 = less popular >>>>>>> than 1, 3 = less so than 2, etc. Array B is the artist info, >>>>>>> scrambled, but each artist is defined by their appropriate >>>>>>> popularity >>>>>>> with a #_ naming convention preceding the artist name and song >>>>>>> title. >>>>>>> I figured that array B had to have some defining characteristic >>>>>>> about >>>>>>> it else there would be no way to order a bunch of String info to a >>>>>>> corresponding rank without knowing the current artist ranking. >>>>>> >>>>>> You say "I figured" so presumably that part of the description is not >>>>>> an >>>>>> explicit part of the original problem definition. How confident are >>>>>> you >>>>>> that B should contain ranking information? If it does, the array A >>>>>> seems >>>>>> superfluous, and the problem is simply one of sorting B. >>>>>> >>>>>> There are alternative interpretations of the rest of your comments >>>>>> that >>>>>> would give meaning to both arrays. >>>>> >>>>> 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? >>> >>>>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[]. >>> >>> - Thee Chicago Wolf [MVP] >> >>Are you certain that the integer values in the A array are already in sort >>order? My intuition is that they are not and that you must sort them. In >>the process of sorting them the elements in the A array will change or >>swap >>their position. Every time this happens your code must swap the >>corresponding elements in the B array. This will keep the A and B arrays >>in >>synchronization. >> >>If my intuition is wrong then I do not understand you problem as explained >>thus far. > > 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. Then all you need to do is sort the song array based upon the first character of the string.