1

Why doesn’t the array class have a .sort()? I don't know how to sort an array directly.

The class array.array is a packed list which looks like a C array. I want to use it because only numbers are needed in my case, but I need to be able to sort it. Is there some way to do that efficiently?

4
  • Maybe numpy is appropriate? stackoverflow.com/questions/5540148/… Commented Feb 5, 2017 at 1:56
  • @Ryan it't a good idea,but I still wonder why array.array() hasn't .sort(), Commented Feb 7, 2017 at 12:09
  • @Ryan , an array which can compactly represent an array of basic values, that means no py_object,just new int[],short[] or something else,it can be fast.if you want sort,just call qsort() in C. so I'm ……confused Commented Feb 7, 2017 at 12:22
  • Yeah, I don’t know why it doesn’t. Commented Feb 7, 2017 at 12:39

2 Answers 2

0

I cannot see a justification why this is missing, and this is really against the batteries included philosophy of Python. A sort function with a key argument would be really helpful to sort arrays of indices pointing to something else.

To sort an array a you could of course do:

a = array.array("L", sorted(a))

but that would not be in place and would cost quite some memory and runtime, converting the array to a list and back to an array.

Sign up to request clarification or add additional context in comments.

Comments

-7

A list is a data structure that has characteristics which make it easy to do some things. An array is a very well understood standard data structure and isn't optimized for sorting. An array is basically a standard way of storing the product of sets of data. There hasn't ever been a notion of sorting it.

10 Comments

“A list is a data structure that has characteristics which make it easy to do some things.” That’s not very specific. “An array is a very well understood standard data structure and isn't optimized for sorting.” What. “An array is basically a standard way of storing the product of sets of data. There hasn't ever been a notion of sorting it.” What.
I thank you for the first bit as it is constructive. However I'm afraid you'll have to be more specific for the second complaint. An array is a standard data structure.
Specifically: “isn’t optimized for sorting” was the part I took issue with. There are tons of algorithms for sorting sequences with constant-time random read and write, i.e. arrays. Maybe you’re talking about a different kind of array with a definition closer to “a standard way of storing the product of sets of data”? array.array (what the question seems to be about) is just a packed list, though.
But again an array is a standard variable that takes an natural number or Cartesian product of them and out puts or maps to a some other object. Any implementation typically must produce this functionality to be what we think of when we think "array". Nothing further is required.
"An array is basically a standard way of storing the product of sets of data." Nope. Thats plain wrong. An array in python is a linear sequence of integer values allowing constant time random access. This has nothing to do with products or sets or Cartesian products.
|

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.