Home All Groups Group Topic Archive Search About

sort two arrays as one?

Author
27 Nov 2007 6:38 PM
BobAchgill
I have 2 arrays with identical  number or rows.

arrword() as string
arrnumber() as integer

How can I best sort  both arrays together using the integer array as the
column to sort by in descending numerical order?


This is what I want to achieve:
1     alive
1     beginning
1     covered
4     along
6     all
13   of
17   earth          
62  and

I tried putting the two arrays into a third single column string array and
used array.sort(ThirdArray) but got these not so good results.  This does not
achieve my numberical sort requirement

This is not what I want:
1   alive                
1   beginning
1   covered
13 of
17 earth
4   along
6   all
62 and

Author
27 Nov 2007 6:58 PM
Tom Shelton
On Nov 27, 11:38 am, BobAchgill <BobAchg***@discussions.microsoft.com>
wrote:
Show quoteHide quote
> I have 2 arrays with identical  number or rows.
>
> arrword() as string
> arrnumber() as integer
>
> How can I best sort  both arrays together using the integer array as the
> column to sort by in descending numerical order?
>
> This is what I want to achieve:
> 1     alive
> 1     beginning
> 1     covered
> 4     along
> 6     all
> 13   of
> 17   earth         
>  62  and
>

What is the relationship between the two arrays?

--
Tom Shelton
Author
27 Nov 2007 7:28 PM
BobAchgill
one to one

e.g.

There is 1 "alive" word
There are 17 "earth" words
There are 62 "and" words

These represent statistics of how many times these words occur in a portion
of text.  I just want  to sort the data into a list of most occuring to least
occuring words based on the word count column.  Each word has one associated
word count number in the other array.

Show quoteHide quote
"Tom Shelton" wrote:

> On Nov 27, 11:38 am, BobAchgill <BobAchg***@discussions.microsoft.com>
> wrote:
> > I have 2 arrays with identical  number or rows.
> >
> > arrword() as string
> > arrnumber() as integer
> >
> > How can I best sort  both arrays together using the integer array as the
> > column to sort by in descending numerical order?
> >
> > This is what I want to achieve:
> > 1     alive
> > 1     beginning
> > 1     covered
> > 4     along
> > 6     all
> > 13   of
> > 17   earth         
> >  62  and
> >
>
> What is the relationship between the two arrays?
>
> --
> Tom Shelton
>
Author
27 Nov 2007 7:43 PM
Tom Shelton
On Nov 27, 12:28 pm, BobAchgill <BobAchg***@discussions.microsoft.com>
wrote:
> one to one
>
> e.g.
>
> There is 1 "alive" word
> There are 17 "earth" words
> There are 62 "and" words
>
> These represent statistics of how many times these words occur in a portion
> of text.  I just want  to sort the data into a list of most occuring to least
> occuring words based on the word count column.  Each word has one associated
> word count number in the other array.
>

Ok... so they have cooresponding indexes... they are parrallel
arrays.  Ok, that makes some sense. now i can think on a solution :)

--
Tom Shelton
Author
27 Nov 2007 9:39 PM
Kerry Moorman
BobAchgill,

You can sort 2 parallel arrays with Array.Sort, like this:

Array.Sort(arrnumber, arrword)

However, for words associated with the same number, this won't sort in
ascending order by word. So you might get this:

1     covered
1     beginning
1     alive
4     along
6     all
13   of
17   earth          
62  and

Kerry Moorman

Show quoteHide quote
"BobAchgill" wrote:

> I have 2 arrays with identical  number or rows.
>
> arrword() as string
> arrnumber() as integer
>
> How can I best sort  both arrays together using the integer array as the
> column to sort by in descending numerical order?
>
>
> This is what I want to achieve:
> 1     alive
> 1     beginning
> 1     covered
> 4     along
> 6     all
> 13   of
> 17   earth          
>  62  and
>
> I tried putting the two arrays into a third single column string array and
> used array.sort(ThirdArray) but got these not so good results.  This does not
> achieve my numberical sort requirement
>
> This is not what I want:
> 1   alive                
> 1   beginning
> 1   covered
> 13 of
> 17 earth
> 4   along
> 6   all
> 62 and
>
>
Author
28 Nov 2007 12:31 AM
Tom Shelton
On Nov 27, 2:39 pm, Kerry Moorman
<KerryMoor***@discussions.microsoft.com> wrote:
Show quoteHide quote
> BobAchgill,
>
> You can sort 2 parallel arrays with Array.Sort, like this:
>
> Array.Sort(arrnumber, arrword)
>
> However, for words associated with the same number, this won't sort in
> ascending order by word. So you might get this:
>
> 1     covered
> 1     beginning
> 1     alive
> 4     along
> 6     all
> 13   of
> 17   earth         
> 62  and
>
> Kerry Moorman
>
>
>
> "BobAchgill" wrote:
> > I have 2 arrays with identical  number or rows.
>
> > arrword() as string
> > arrnumber() as integer
>
> > How can I best sort  both arrays together using the integer array as the
> > column to sort by in descending numerical order?
>
> > This is what I want to achieve:
> > 1     alive
> > 1     beginning
> > 1     covered
> > 4     along
> > 6     all
> > 13   of
> > 17   earth         
> >  62  and
>
> > I tried putting the two arrays into a third single column string array and
> > used array.sort(ThirdArray) but got these not so good results.  This does not
> > achieve my numberical sort requirement
>
> > This is not what I want:
> > 1   alive               
> > 1   beginning
> > 1   covered
> > 13 of
> > 17 earth
> > 4   along
> > 6   all
> > 62 and- Hide quoted text -
>
> - Show quoted text -

Funny...  I never noticed that array.sort allowed that.  Well, you
learn something new every day.

--
Tom Shelton
Author
28 Nov 2007 10:43 PM
ShaneO
Tom Shelton wrote:
>
> Funny...  I never noticed that array.sort allowed that.  Well, you
> learn something new every day.
>
Yes, I only discovered this recently myself while trying to find a way
to sort Structures based on any Member of the Structure.

I found it very difficult to find a solution, so if anyone else is
looking for the same I'd be happy to provide the code.

Maybe the OP would be interested in merging his two (or more) arrays
into a Structure Array?

ShaneO

There are 10 kinds of people - Those who understand Binary and those who
don't.
Author
28 Nov 2007 1:31 AM
Terry
You could build yourself a 2 column DataTable...
Dim dt As New DataTable
dt.Columns.Add(New DataColumn("TheCount", Type.GetType("System.Int32")))
dt.Columns.Add(New DataColumn("TheWord", Type.GetType("System.String")))
....
Load the rows,
....
and then sort...
dt.DefaultView.Sort = "TheCount, TheWord"

--
Terry


Show quoteHide quote
"BobAchgill" wrote:

> I have 2 arrays with identical  number or rows.
>
> arrword() as string
> arrnumber() as integer
>
> How can I best sort  both arrays together using the integer array as the
> column to sort by in descending numerical order?
>
>
> This is what I want to achieve:
> 1     alive
> 1     beginning
> 1     covered
> 4     along
> 6     all
> 13   of
> 17   earth          
>  62  and
>
> I tried putting the two arrays into a third single column string array and
> used array.sort(ThirdArray) but got these not so good results.  This does not
> achieve my numberical sort requirement
>
> This is not what I want:
> 1   alive                
> 1   beginning
> 1   covered
> 13 of
> 17 earth
> 4   along
> 6   all
> 62 and
>
>