#Sorting by highest to lowest value from a given array into a new array

1 messages · Page 1 of 1 (latest)

ashen zephyr
#

static public int getIdxOfMaxValue(int[][] pairs) {
if (pairs == null || pairs.length == 0) {
return -1;
}
int maxIdx = 0;
int maxVal = pairs[0][1]

            for (int i = 1; i < pairs.length; i++) {
                if pairs[i][1] > maxVal {
                    maxVal = pairs[i][1];
                    maxIdx = i;
                }
                        return maxIdx;
            }

/**
*Sorts key-value pairs by value by writing the largest value with its key to a new
*array. The method getIdxOfMaxValue() should be used here. <br>
* If required, Integer.MIN_VALUE can be used here.
*
* @param pairs several pairs in an array (one column = one pair); the key is at the first index,
* the value at the second index
* @return sorted Array of pairs
*/

static public int[][] sortPairs(int[][] pairs) {
if (pairs == null || pairs.length == 0) {
return new pairs[0][0];
}

}

Hey guys, i need a hint or some advice with this method. I don't know how to write the code/the steps. I've already written the method getIdxOfMaxValue.

odd ruinBOT
#

<@&987246399047479336> please have a look, thanks.

plucky rivet
#

I'm assuming this above is what is written in the original problem.
So the hint is using Integer.MIN_VALUE...

what would happen if I did this:

System.out.print(pairs[index][1]+" ");
pairs[index][1] = Integer.MIN_VALUE;```

and then if I did it again...

```int index = getIdxOfMaxValue(pairs);
System.out.print(pairs[index][1]+" ");
pairs[index][1] = Integer.MIN_VALUE;```
austere oasis
#

I recommend using:

odd ruinBOT
#

Please use this format for posting code:

```java
// Example java program
int value = 5;
System.out.println(value);
```

Which results in:

// Example java program
int value = 5;
System.out.println(value);

For syntax highlighting, you have to add the name of the language after the three backticks, like ```java. Please make sure to use exactly this format, so no space between the backticks and the language name, and a newline before the code starts. If done right, the syntax highlighting will even be applied to your text as you type, before sending.

austere oasis
#

Also any reason not to use stream => sorted?

ashen zephyr
echo scarab
#

What is with the code you posted. Is the first method given?

ashen zephyr
#

No

#

I had to write the first method myself

#

And i have to use it for the second method "Pairs"

echo scarab
#

So you have to write a sort algorithm?

#

And the data is stored in the first array as sequential key,value pairs?

ashen zephyr
#

Yes kinda, i have to sort key-value pairs by value by writing the largest value with its key to a new Array, i have to use the already implemented method "getIdxOfMaxValue()" to get the Index of the highest Value

#

Should i just make a copy of the passed array to the method in order to create a new array in which i can write (later) the values in a sorted order & then use the method 'getIdxOfMaxValue()'?

echo scarab
#

Does getIdxOfMaxValue even work correctly?

ashen zephyr
ashen zephyr
#

Here is the JavaDoc for getIdxOfMaxValue:

/**
* Determines the index of the pair with the largest value.
*
* @param pairs several pairs in an array; the key is at the first index,
* the value of the pair at the second index.
* Since we are only interested in the highest value, the respective key
* is not of interest.
* @return index of the pair containing the largest value, or -1 if the array is empty
*/

echo scarab
#

Did you write that javadoc?

ashen zephyr
#

We should implement the Method accordingly

#

All the Methods according to the JavaDoc

echo scarab
#

Are you sure the data is not supposed to be just one array, where keys are on even indexes and values on odd indexes?

ashen zephyr
echo scarab
#

Do you understand why Integer.MIN_VALUE is hinted at?

ashen zephyr
mental perch
#

lowest number possible so it acts as baseline

ashen zephyr
ashen zephyr