Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!nx01.iad01.newshosting.com!newshosting.com!news-out.readnews.com!news-xxxfer.readnews.com!s09-11.readnews.com!unm2.readnews.com.POSTED!not-for-mail X-Trace: DXC=>8SHENN_ojk:WCTbXQ[dcfk\2KDSF=2@bFV0LA4MIjQkmTIDj6@1Wal^]]CW6JBjaiTA`X]=O@ZUnOB1ej7cfI3iQRlGN[[]R5fo3R^HBiTkAh:Wa6U]`:5EbLoRkGiP7ZWc X-Complaints-To: killthespammer@usenetmonster.com From: "Thee Chicago Wolf [MVP]" <.@.> Newsgroups: comp.lang.java.help Subject: Re: How to sort String array A based on int array B Date: Fri, 23 Sep 2011 09:13:47 -0500 Organization: . Reply-To: . Message-ID: References: <1k2n77ltdv7ml661pa9a2g63j8q5l1c66g@4ax.com> X-Newsreader: Forte Agent 6.00/32.1186 X-No-Archive: yes MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Lines: 102 NNTP-Posting-Host: f491fd15.newsreader.readnews.com Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.help:1113 >On 9/22/2011 3:31 PM, Thee Chicago Wolf [MVP] wrote: >> Hi all, >> >> I was given the homework task of sorting a String array A based on int >> array B *without* using java.util.Arrays or sort(). It's gotta be done >> old school using sort method such as Bubble or Selection sort. I'm >> gong with Bubble. >> >> So I have two arrays like so: >> >> int[] rank = {1,2,3,4,5,6}; >> >> String[] artist = {"6_artist1", >> "4_artist2", >> "1_artist", >> "3_artist", >> "2_artist", >> "5_artistl"}; >> >> I get the gist of the sort loop is going to look somewhat like so: >> >> int temp = 0; >> >> for(int i = 0; i< arrayLength; i++) >> { >> for(int j = 1; j< (arrayLength - i); j++) >> { >> //if (Integer.valueOf(charAt(0)) == rank[i]) >> if(a[j - 1]> a[j]) >> { >> temp = a[j - 1]; >> a[j - 1] = a[j]; >> a[j] = temp; >> } >> } >> } >> >> The trouble I am having is sorting array B based on array A. One's a >> String the other's an int. I thought I could use something like >> (Integer.valueOf(charAt(artist[i])) to do it. Am I on the right track >> with somethig like that? > > I doubt it. "Based on" isn't entirely clear, but I'm guessing >it probably means that rank[k] and artist[k] are to be thought of as >"associated" in some way, so that if you start with > > rank artist > 9 Bergonzi > 1 Pavarotti > 4 Kraus > 5 Di Stefano > 7 Vanzo > >... you would end up with > > rank artist > 1 Pavarotti > 4 Kraus > 5 Di Stefano > 7 Vanzo > 9 Bergonzi > >That is, the rank[] array has been sorted normally, but the artist[] >array has also been scrambled in exactly the same way. If rank[j] >moved to rank[k] in the sort, artist[j] moved to artist[k] similarly. Yes, rank[k] is already sorted from most popular to least popular (1 being most popular and 6 being least). artist [k] is scrambled (read: unsorted) but *does* have its know rank preceding it (i.e., 6_ or 4_). After all, how could you sort a bunch of Artists (read: String items) without already knowing their current / existing ranking? To me that doesn't seem possible. Or I just don't know how to do that (yet). ^_^ So I want to sort artist [k] based on the already defined order of rank [k]. So, as you have it above with Pavarotti ranked 1st, Kraus 2nd, etc. but the sorting order would be based on the order in rank []. But of course, one rank [] is an int and artist [] is String. Incompatible types. I thought that I could use something like (Integer.valueOf(charAt(artist[i])) or is it (Integer.valueOf(charAt(0(artist[i]))) to get the first character of that string item (since it is a number) and check it against the number in rank [k]. If it matches, great, that item stays and we move onto the next element / index. If not, we resort. Does that seem right? Am I overcomplicating it? >If that's what you mean by "based on," then the problem is really >just one of sorting the rank[] array by whatever means you like, with >the added fillip that whenever you exchange rank[i] <-> rank[j] you >also exchange artist[i] <-> artist[j]. The artist[] elements do not >participate in the sorting at all; they're just "along for the ride." > >If that's not what you mean by "based on," please explain more >fully. I'm still learning so explaining it in Noobese is, I'm sure, confusing for those out there who already know way more than I do but aren't able to extrapolate what is going on in my head. Thanks for helping me out though. - Thee Chicago Wolf [MVP]