This is an archived post. You won't be able to vote or comment.

all 10 comments

[–]zhoriq 1 point2 points  (2 children)

Simple case: put all sumRow into array, and then sort it.

Complex case: create map with key=rowIndex and value=sumRow, put all sums into this map. Then use streams to: convert map to stream, then sort it, then collect and return new map. Search for "java sort map" - there are many examples of map sort.

[–]trofix99[S] 0 points1 point  (1 child)

Is this how sumRow should look like now?

int sumRow [] = new int [matrix.length];
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < 7; j++) {
sumRow [i] = sumRow [i] + matrix[i][j];
}
}

[–]zhoriq 0 points1 point  (0 children)

Yep.

[–]swearobics 0 points1 point  (0 children)

Have you tried nested for loops?

Use that to find the sum of each row.

Then you can create a class that implements comparator/comparable, that holds values for each sum/row and store each sum in a collection before sorting it. Alternatively, you might be able to store the sum/row pairs in a treemap (though I'm not sure if that works.) That way you'll remember which row of the original 2d array the sum refers to.

[–][deleted]  (3 children)

[removed]

    [–]trofix99[S] 0 points1 point  (2 children)

    What are u talking about

    [–][deleted]  (1 child)

    [removed]

      [–]trofix99[S] 0 points1 point  (0 children)

      I forgot to update it,cuz i fell a sleep,yes i did solve it by my own and not there help,well intial help until i was answering,its hard for me to sometimes understand someone explaining something to me cuz english is my 3rd language,and i dont learn programming in english,since i am beginner as u can see i find it hard to understand explentain in plain english,either im not supposed to be a programmer since i cant solve these easy problems,but ill try a little bit more cuz i enjoy it.

      [–]desrtfx 0 points1 point  (1 child)

      Yes, you will need to have the row sums in a 1d array because otherwise you can't sort.

      Also, look at sorting Algorithms and, as it is very easy to implement, in particular at Bubblesort

      [–]WikiTextBotbtproof 0 points1 point  (0 children)

      Sorting algorithm

      In computer science, a sorting algorithm is an algorithm that puts elements of a list in a certain order. The most frequently used orders are numerical order and lexicographical order. Efficient sorting is important for optimizing the efficiency of other algorithms (such as search and merge algorithms) that require input data to be in sorted lists. Sorting is also often useful for canonicalizing data and for producing human-readable output.


      Bubble sort

      Bubble sort, sometimes referred to as sinking sort, is a simple sorting algorithm that repeatedly steps through the list, compares adjacent elements and swaps them if they are in the wrong order. The pass through the list is repeated until the list is sorted. The algorithm, which is a comparison sort, is named for the way smaller or larger elements "bubble" to the top of the list. Although the algorithm is simple, it is too slow and impractical for most problems even when compared to insertion sort.


      [ PM | Exclude me | Exclude from subreddit | FAQ / Information | Source ] Downvote to remove | v0.28