Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.help > #1150

Re: How to sort String array A based on int array B

Path csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder3.hal-mli.net!news.glorb.com!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail
From Lew <lewbloch@gmail.com>
Newsgroups comp.lang.java.help
Subject Re: How to sort String array A based on int array B
Date Tue, 27 Sep 2011 12:25:36 -0700 (PDT)
Organization http://groups.google.com
Lines 125
Message-ID <29089952.226.1317151536694.JavaMail.geo-discussion-forums@prec11> (permalink)
References <822p77tjmlsdrp2r7so1ko70igu9fascn6@4ax.com> <u-CdnSQgGqDnC-HTnZ2dnUVZ_rOdnZ2d@earthlink.com> <e9ap771g34045ad5tda2mp36imru521ks9@4ax.com> <Kd6dnbo1BNGUJuHTnZ2dnUVZ_s6dnZ2d@earthlink.com> <j5np771d6gjjdo1m1cj43fo4e6s3lknb1f@4ax.com> <5PmdnbXtmIHNYOHTnZ2dnUVZ_vCdnZ2d@earthlink.com> <m8cq77pb4h2odihba17kjmdeb1hkhmo115@4ax.com> <vamdnaXs-8hsqeDTnZ2dnUVZ_sednZ2d@earthlink.com> <ijv08799d2u73gpduufeg5gs9qkictcpmp@4ax.com> <Zd-dnSM_Q5HpCR3TnZ2dnUVZ_sOdnZ2d@earthlink.com> <io5487pllretr3qukbtebipbcu0k6fnah0@4ax.com>
Reply-To comp.lang.java.help@googlegroups.com
NNTP-Posting-Host 2620:0:1000:fd42:224:d7ff:fe69:5838
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1
X-Trace posting.google.com 1317151648 27683 127.0.0.1 (27 Sep 2011 19:27:28 GMT)
X-Complaints-To groups-abuse@google.com
NNTP-Posting-Date Tue, 27 Sep 2011 19:27:28 +0000 (UTC)
In-Reply-To <io5487pllretr3qukbtebipbcu0k6fnah0@4ax.com>
Complaints-To groups-abuse@google.com
Injection-Info glegroupsg2000goo.googlegroups.com; posting-host=2620:0:1000:fd42:224:d7ff:fe69:5838; posting-account=CP-lKQoAAAAGtB5diOuGlDQk0jIwmH0T
User-Agent G2/1.0
X-Google-Web-Client true
Xref x330-a1.tempe.blueboxinc.net comp.lang.java.help:1150

Show key headers only | View raw


Thee Chicago Wolf [MVP] wrote:
>> Patricia Shanahan wrote (as you should have attributed):
>>>> 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.
> 
> Nope, no requirement. But think about this. If the prof said make
> array A int[] rank = {6,1,5,2,4,3}; then what you say about by just
> sorting B fails. A will always determine how B will be sorted. If A is
> changed, B will change accordingly. Does that make sense?

Patricia did say that when A changes, that B changes accordingly, as did others.  But that does not make fail what she said about sorting B.  It validates what she said about sorting B.

>>>> 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

Wrong.  You are the one failing to see what she means, not vice versa.

>>> ordered randomly but by popularity so you already *know* their rank as
>>> a byproduct of their popularity.

You don't know their rank until you look up the 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.
> 
> Would that not be {"zzz", "xxx", "yyy"}. What does a code example of

No.  The rank array corresponds by index to the song-title array.  So A[0] is the rank of B[0].  By the way, those arrays should not be named 'A' and 'B', but 'a' and 'b'. 

So a[0] (3) is the rank of b[0] ("xxx").  Therefore "xxx" should be the third element of the sorted version of 'b'.

Patricia is correct.  

> the above look like so I know for the future?

It looks like sorting 'a' while performing the exact same swaps in 'b' as you do in 'a', as stated upthread a couple or few times already.

> Using a method, I solved my problem like so:
> 
> for (int b = 0; b < a.length; i++)
> 	{

Indent the opening brace with the control statement, i.e., lined up with the 'for' above it.

> 	for (int c = 0; c < title.length; j++)

No.

> 		{

One less indent level for that curly brace.

> 		int firstItem = (int)title[c].charAt(0)-'0';

*DO NOT USE TAB CHARACTERS TO INDENT SOURCE ON USENET!*

> 		if (firstItem == a[b])
> 			{
> 			songSorted[temp] = title[c];
> 			temp++;
> 			}
> 		}
> 	}
> 	return songSorted;

http://sscce.org/

As presented, your code does not compile.

It also solves the problem wrong.

>> 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.
> 
> Right, I see what you mean. The assignment only said Sort a secondary
> array B based on the sorting order of A. It didn't say HOW you had to
> do that. As far as I am concerned, if I write something that sorts
> array B based on the order of---no matter how I do it-- and it does
> what the requirement specifies, the requirement has been met. It's the
> prof's job, not mine, to add granularity to the requirement. If it
> said get a bucket of water and I bring back a bucket of water, problem
> solved. ^_^

I would be interested in the exact (verbatim) wording of the problem statement, to eliminate the ambiguity in your interpretation of "basede on the sorting order of A" vs. everyone else's.

-- 
Lew

Back to comp.lang.java.help | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

How to sort String array A based on int array B "Thee Chicago Wolf [MVP]" <.@.> - 2011-09-22 14:31 -0500
  Re: How to sort String array A based on int array B markspace <-@.> - 2011-09-22 15:38 -0700
    Re: How to sort String array A based on int array B "Thee Chicago Wolf [MVP]" <.@.> - 2011-09-23 08:51 -0500
      Re: How to sort String array A based on int array B Patricia Shanahan <pats@acm.org> - 2011-09-23 07:23 -0700
        Re: How to sort String array A based on int array B "Thee Chicago Wolf [MVP]" <.@.> - 2011-09-23 10:51 -0500
          Re: How to sort String array A based on int array B Patricia Shanahan <pats@acm.org> - 2011-09-23 09:59 -0700
            Re: How to sort String array A based on int array B "Thee Chicago Wolf [MVP]" <.@.> - 2011-09-23 14:31 -0500
              Re: How to sort String array A based on int array B Patricia Shanahan <pats@acm.org> - 2011-09-23 12:39 -0700
              Re: How to sort String array A based on int array B "Charles Hottel" <chottel@earthlink.net> - 2011-09-23 17:42 -0400
                Re: How to sort String array A based on int array B "Thee Chicago Wolf (MVP)" <.@.> - 2011-09-23 20:27 -0500
                Re: How to sort String array A based on int array B Patricia Shanahan <pats@acm.org> - 2011-09-23 18:39 -0700
                Re: How to sort String array A based on int array B "Thee Chicago Wolf (MVP)" <.@.> - 2011-09-26 08:37 -0500
                Re: How to sort String array A based on int array B Patricia Shanahan <pats@acm.org> - 2011-09-26 08:04 -0700
                Re: How to sort String array A based on int array B "Thee Chicago Wolf [MVP]" <.@.> - 2011-09-27 13:47 -0500
                Re: How to sort String array A based on int array B "Thee Chicago Wolf [MVP]" <.@.> - 2011-09-27 13:59 -0500
                Re: How to sort String array A based on int array B Lew <lewbloch@gmail.com> - 2011-09-27 12:25 -0700
                Re: How to sort String array A based on int array B Lew <lewbloch@gmail.com> - 2011-09-26 09:00 -0700
                Re: How to sort String array A based on int array B "Charles Hottel" <chottel@earthlink.net> - 2011-09-23 23:31 -0400
                Re: How to sort String array A based on int array B "Thee Chicago Wolf (MVP)" <.@.> - 2011-09-26 08:29 -0500
  Re: How to sort String array A based on int array B Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-09-22 22:28 -0400
    Re: How to sort String array A based on int array B "Thee Chicago Wolf [MVP]" <.@.> - 2011-09-23 09:13 -0500
      Re: How to sort String array A based on int array B Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-09-23 20:45 -0400
  Re: How to sort String array A based on int array B Roedy Green <see_website@mindprod.com.invalid> - 2011-09-23 02:28 -0700
    Re: How to sort String array A based on int array B "Thee Chicago Wolf [MVP]" <.@.> - 2011-09-23 09:14 -0500
  Re: How to sort String array A based on int array B Roedy Green <see_website@mindprod.com.invalid> - 2011-09-25 00:46 -0700

csiph-web