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


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

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

From Eric Sosman <esosman@ieee-dot-org.invalid>
Newsgroups comp.lang.java.help
Subject Re: How to sort String array A based on int array B
Date 2011-09-22 22:28 -0400
Organization A noiseless patient Spider
Message-ID <j5gqu1$16g$1@dont-email.me> (permalink)
References <1k2n77ltdv7ml661pa9a2g63j8q5l1c66g@4ax.com>

Show all headers | View raw


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.

     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.

-- 
Eric Sosman
esosman@ieee-dot-org.invalid

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