Sorting integers in an array
I am making an application that displays the highest score in one range of numbers, and displays the highest score in another range of numbers. I have a 2 dimensional array built and am wondering how to sort them then get the top number to display.
this is the statement I use:
Array.Reverse(scores(1, column))
This ensures that I take from the correct column, but the problem I have is I get a message stating for the underlined code:
value of type 'Integer' cannot be converted to System.Array
I'm confused because I thought that scores(,) is an array already.
Thanks in advance for any help.
Re: Sorting integers in an array
Assuming that you initialised the array 'scores' elsewhere, when you type scores(1, column) you are actually retrieving the integer stored at location (1, column) in your array. What you'll want to do is: Array.Reverse(scores).
Alternatively, have you taken a look at Array.Sort()?
Re: Sorting integers in an array
How would I go about sorting only one column at a time?
(arrays are so confusing to me, lol)
Re: Sorting integers in an array
First up, you're getting that error message because you are indexing your array and getting a single element, then passing that to Reverse. The single element is an Integer while Reverse expects an array, hence the error message.
Have you read the documentation for Array.Reverse? I'm guessing not. There are two overloads and both expect you to pass an Array. The second lets you specify a start and end index so that only a portion of the array is reversed. In BOTH cases the description of the method SPECIFICALLY states that it operates on a one-dimensional array.
The simple fact is that multi-dimensional arrays should be used very, very rarely. They should only ever be used when each element is a peer to the others. An example might be if you wanted to represent the desks in a class room, where they would be in a matrix formation. You should NEVER use a multidimensional array where one "row" or "column" means something different to another "row" or "column".
If you have an array of high scores then I can't see how a 2D array could possibly apply. The scores could only be in a simple list from first to last. I'm guessing that you have one "column" of names and another "column" of scores. This is a prime example of where elements are NOT peers. A name and a score represent two completely different things, so they should not in the same array.
What you should do in cases like that is declare a custom type to group related data items, e.g. a structure with a Name property and a Score property. You would then create multiple instances of that type and then create a simple list of those objects. You can then easily sort those objects by their Scores property values.
EDIT: Note that Array.Sort will only work with 1D arrays too.
Re: Sorting integers in an array
One column is midterm grades and the other is final grades, sorry I didn't specify. I am doing this as a homework assignment, please don't think I'm trying to bum code off you though. I know it is a wierd request to make a 2d array for something such as this, if I were to code this for personal use it would definitely be 2 one dimensional arrays.
So you can't sort columns of a 2D array?
Re: Sorting integers in an array
I reality multidimensional arrays are rarely used. The examples used to teach their use are often relatively manufactured as a result. There is no inherent method of sorting multidimensional arrays, nor should there be. If multidimensional arrays are used properly then sorting them doesn't make sense. As a result, you're just going to have to use loops to sort the array yourself.