I have an array of items as follows in Javascript:
var users = Array();
users[562] = 'testuser3';
users[16] = 'testuser6';
users[834] = 'testuser1';
users[823] = 'testuser4';
users[23] = 'testuser2';
users[917] = 'testuser5';
I need to sort that array to get the following output:
users[834] = 'testuser1';
users[23] = 'testuser2';
users[562] = 'testuser3';
users[823] = 'testuser4';
users[917] = 'testuser5';
users[16] = 'testuser6';
Notice how it is sorted by the value of the array and the value-to-index association is maintained after the array is sorted (that is critical). I have looked for a solution to this, tried making it, but have hit a wall.
By the way, I am aware that this is technically not an array since that would mean the indices are always iterating 0 through n where n+1 is the counting number proceeding n. However you define it, the requirement for the project is still the same. Also, if it makes a difference, I am NOT using jquery.
If I understand the question correctly, you're using arrays in a way they are not intended to be used. In fact, the initialization style
teaches wrong things about the nature and purpose of arrays. An array is an ordered list of items, indexed from zero up. The right way to create an array is with an array literal:
The indexes are implied based on the order the items are specified. Creating an array and setting
users[562] = 'testuser3'
implies that there are at least 562 other users in the list, and that you have a reason for only knowing the 563rd at this time.In your case, the index is data, and is does not represent the order of the items in the set. What you're looking for is a map or dictionary, represented in JavaScript by a plain object:
Now your set does not have an order, but does have meaningful keys. From here, you can follow galambalazs's advice to create an array of the object's keys:
…then sort it:
Here's a demo
Sparse arrays usually spell trouble. You're better off saving key-value pairs in an array as objects (this technique is also valid JSON):
As suggested, you can use a for loop to map the sorting function onto the array.
The order of the elements of an array is defined by the index. So even if you specify the values in a different order, the values will always be stored in the order of their indices and undefined indices are
undefined
:Now if you want to store the pair of index and value, you will need a different data structure, maybe an array of array like this:
This can be sorted with this comparison function:
The result is this array:
I'd use map once to make a new array of users, then a second time to return the string you want from the new array.
If you want to avoid any gaps. use a simple filter on the output-
Using the ideas from the comments, I came up with the following solution. The naturalSort function is something I found on google and I modified it to sort a multidimensional array. Basically, I made the users array a multidimensional array with the first index being the user id and the second index being the user name. So:
I then sorted the array to get the following output:
The code to do this is below:
Array.prototype.sort()
takes an optional custom comparison function -- so if you dump all of yourusers
into an array in this manner[ [562, "testuser3"], [16, "testuser6"] ... etc.]
Then
sort
this array with the following function:Then rebuild your
users
object. (Which will loose you your sorting.) Or, keep the data in the newly sorted array of arrays, if that will work for your application.