0

I have an array A of the form given below:

A = [[ 4  3  2  1]
     [ 8  7  6  5]
     [12 11 10  9]
     [16 15 14 13]]

I would like to sort this array by row. The output required is:

A = [[ 1  2  3  4]
     [ 5  6  7  8]
     [ 9 10 11 12]
     [13 14 15 16]]

I tried using the following code (but it does not work):

import numpy as np
A = np.array([[4, 3, 2, 1], [8, 7, 6, 5], [12, 11, 10, 9], [16, 15, 14, 13]])
print A[np.lexsort(A, axis = 1)]

How do I sort this array by row?

3
  • 1
    You should make your code syntactically valid. Commented Jun 22, 2015 at 18:36
  • @juanchopanza definitely. but how can you expect that there ware the commas missing and not the brackets surplus? Commented Jun 22, 2015 at 18:38
  • 1
    An array has only one row, right? Commented Jun 22, 2015 at 18:39

4 Answers 4

3

It isn't clear what you mean by "sorting by row", but you seem to want to sort the elements of the array, which you can do by iterating over it and sorting each element:

for i in A: i.sort()

If you also want to sort the outer array itself, then, well, sort it:

A.sort()
Sign up to request clarification or add additional context in comments.

2 Comments

I wouldn't add this (confusing) comment about the outer array. The asker already added the expected behaviour.
@Wolf I think OP under-specified the requirements.
1

Numpy's sort has an axis argument that can be used to specify which axis is sorted.

import numpy as np
A = np.array([[4, 3, 2, 1], [8, 7, 6, 5], [12, 11, 10, 9], [16, 15, 14, 13]])

A.sort(axis=1)

which gives:

[[ 1  2  3  4]
 [ 5  6  7  8]
 [ 9 10 11 12]
 [13 14 15 16]]

sort defaults to use axis as the last axis of the array, so here it would be 1, so you don't really need to specify axis=1 in this case.

Comments

0
A = [[ 4,  3,  2,  1],
     [12, 11, 10,  9],
     [ 8,  7,  6,  5],
     [16, 15, 14, 13]]
for i in A:
    i.sort()

A.sort()
print A

I assumed row by row means you want to sort outer list also

3 Comments

@Wolf - sorry I am not aware of that :)
@Wolf By the way I am not rep hunting I happened to post the same answer by coincidence. Am I supposed to delete it now. please enlighten me I am really new to SO.
It's up to you. Your answer looks probably more pragmatic with the repeated (and corrected!) question part.
0

Two different answers, both one-liners, are as follows:

sorted_A_1 = sorted(sorted(i) for i in A)
sorted_A_2 = [sorted(i) for i in sorted(A)]

While you may want to simply modify the existing list, these both generate a new list. The first one pipes the result of sorting each list in A to the sorted function and the second is just a list comprehension.

Also, while these may not be clearer than the answers already posted, I personally think it's helpful to see different approaches to solving a given problem!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.