Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Given a matrix, sort the rows (either in ascending or descending order) based on the values of column (column index given as an input to the function). #159

Open
3 of 4 tasks
chichra opened this issue Apr 14, 2019 · 13 comments
Labels
enhancement New feature or request up-for-grabs

Comments

@chichra
Copy link

chichra commented Apr 14, 2019

Do the checklist before filing the issue:

  • Do you have Node.js and all the relevant dev-dependencies installed?
  • Is this a bug fix?
  • Is this an enhancement?
  • Is this a feature request?
NOTE: Provide a clear and concise description of the feature that needs to be added! Or if its a bug, then provide the necessary steps to reproduce it along with screenshots.
  • Give your answer below:

@chichra
Copy link
Author

chichra commented Apr 14, 2019

@pbiswas101 Similarly, this can be extended to sorting the columns of a matrix based on a row index.

For example , given matrix
M =
[[3 2 1]
[4 6 5]
[3 4 2]]

Sorting the rows by last column gives.

M =

[[3 2 1]
[3 4 2]
[4 6 5]]

@fave77
Copy link
Owner

fave77 commented Apr 15, 2019

@chichra can you be more specific on this:

sort the rows based on the values of column

I mean are you talking about just swapping two rows? and how do the sorting works? In the example you mentioned, what if the last row was [ 3 4 7 ] and how this would affect the outcome!

@viditkumar
Copy link
Contributor

@pbiswas101 I suppose, she is talking about swapping the two rows. If the last row was [ 3 4 7 ], please look at the below example.

M =
[[3 2 1]
[4 6 5]
[3 4 7]]

Sorting the rows by last column gives.
M =
[[3 2 1]
[4 6 5]
[3 4 7]]

There is no swapping required in the above scenario.
If this issue is fine and gets accepted, I would like to work on it.

@sakshichahal53
Copy link
Contributor

For sorting rows by a column number let say 2, we have to look upon sorting the numbers in the given column. And accordingly the rows are swapped.
Given :
M= [[1 2 10],
[4 6 5],
[2 4 7]]

Sorting rows by 1 column gives: Since numbers in first column are 3 4 3 sorted = (3 3 4), we have
A= [[1 2 10],
[2 4 7],
[4 6 5]]

Sorting rows by 2 column gives: Since numbers in second column are 2 6 4 sorted = (2 4 6), we have
B= [[1 2 10],
[2 4 7],
[4 6 5]]

Sorting rows by 3 column gives: Since numbers in third column are 10 7 5 sorted = (5 7 10), we have
C= [[4 6 5],
[2 4 7],
[1 2 10]]

@chichra I believe this is what you suppose to mean?

@chichra
Copy link
Author

chichra commented Apr 15, 2019

@sakshichahal53 yes absolutely, that is what I wanted.

@chichra
Copy link
Author

chichra commented Apr 15, 2019

@pbiswas101 as I mentioned earlier that similarly, this can be extended to sorting the columns of a matrix based on a row index.

For example , given matrix the same matrix, If i wish to sort (rather rearrange) the columns of matrix based on the values of row 1

Original matrix is
M =
[[3 2 1]
[4 6 5]
[3 4 2]]

Sorting the columns by row indices gives.

sorted matrix M(based on row 1)=
[[1 2 3]
[5 6 4]
[2 4 3]]

sorted matrix M (based on row 2)=
[[3 1 2]
[4 5 6]
[3 2 4]]

sorted matrix M (based on row 3)=
[[1 3 2]
[5 4 6]
[2 3 4]]

@Shubhayu-Das
Copy link
Contributor

I have a doubt: How will this help in competitive programming?

@fave77
Copy link
Owner

fave77 commented Apr 16, 2019

@chichra will you be working on this issue?

@fave77 fave77 added enhancement New feature or request up-for-grabs labels Apr 16, 2019
@chichra
Copy link
Author

chichra commented Apr 16, 2019

@pbiswas101 yes

@fave77
Copy link
Owner

fave77 commented Apr 16, 2019

@chichra okay, how are you planning to implement this?
please provide code snippets so as to show the usage of this function in terms of a matrix a,

const a = new M.Matrix([ [3, 2, 1], [4, 6, 5], [3, 4, 2] ])

@chichra
Copy link
Author

chichra commented Apr 17, 2019

a.sort('row', 0)
output will be
[[1 2 3]
[5 6 4]
[2 4 3]]

a.sort('column', 0)
[[3 2 1]
[3 4 2]
[4 6 5]]

The first parameter in function signature represents if we want to sort based on row/column and the second parameter denotes the index.

@fave77
Copy link
Owner

fave77 commented Apr 17, 2019

@chichra this issue is more or less similar to issue #152 and the sorting also need to include both ascending / descending order as mentioned in the PR #169

So, the overall implementation would be:

a.sort('row', true, 0)

where 2nd and 3rd arguments are optional! If the 3rd argument is to be omitted, then the behavior of sorting would go along as mentioned in issue #152

Hence, once that issue is resolved you may start working on this one, building on top of that implementation.

@chichra
Copy link
Author

chichra commented Apr 17, 2019

Yes, sure.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request up-for-grabs
Projects
None yet
Development

No branches or pull requests

5 participants