snippetjavascriptMinor
Asc and desc array sort methods
Viewed 0 times
arrayascmethodsdescandsort
Problem
I've used the typical INT array sort function found all over the internet for years, but I've also had a need to sort other items in an array, or even to just simple take an
The method (what work it does)
At current, it will sort a variety of items in an array; Ascending or Descending. If there are more than one type of item, then the types are grouped together in a somewhat (at least to me) logical fashion: Ints, Strings, Arrays, Unspecified Objects, Element Objects, Date Objects, nulls & undefines. However, within each group, the items are still sorted based on given parameter.
The simplicity is in only needing to supply 1 to 2 parameters (based on implementation, pure method form needs supplied Array of course). The main parameter is simply a string being
Implementation
Comes in 2 flavors. One, requiring EcmaScript 5.1, simply adds it to
The other is a traditional method, of course. Old fashioned calling like:
Added Ease
Also implemented in both flavors is the ease of use on the String parameter. If no parameter is provided, than 'asc' is assumed.
Also implemented in flavor 2 is the ease of parameter order. It doesn't matter if you place the Array or the String first, as long as, at the least, the
More Perks
Of course, simple things like Ints and Strings are handled in proper manner, including real number sorting as opposed to traditional
Array of multiple types of items and get it in order for later readability. Thus leading me to the smartSort method I list below. If I've anything else I can think to implement at the moment (will update later), it's that I currently don't have a parameter for sorting Objects by a specific property. The method (what work it does)
At current, it will sort a variety of items in an array; Ascending or Descending. If there are more than one type of item, then the types are grouped together in a somewhat (at least to me) logical fashion: Ints, Strings, Arrays, Unspecified Objects, Element Objects, Date Objects, nulls & undefines. However, within each group, the items are still sorted based on given parameter.
The simplicity is in only needing to supply 1 to 2 parameters (based on implementation, pure method form needs supplied Array of course). The main parameter is simply a string being
'asc' or 'desc'.Implementation
Comes in 2 flavors. One, requiring EcmaScript 5.1, simply adds it to
Array.prototype. Thus making it naturally easy to call, such as: myArray.smartSort('asc').The other is a traditional method, of course. Old fashioned calling like:
smartSort(myArray, 'asc').Added Ease
Also implemented in both flavors is the ease of use on the String parameter. If no parameter is provided, than 'asc' is assumed.
Also implemented in flavor 2 is the ease of parameter order. It doesn't matter if you place the Array or the String first, as long as, at the least, the
Array exist.More Perks
Of course, simple things like Ints and Strings are handled in proper manner, including real number sorting as opposed to traditional
.sort of JS. However, this will also sort Arrays and Objects based on items/properties, after sorting those accordingly. For example, if you have 3 arrays Solution
Naming
Using single-letter variable names is bad, and there is absolutely no reason to use them as we aren't limited on name length anymore. Looking through your code, I still don't know what
Sorting
I assume
Functions
You don't need to all related code in one function. You should split your code up into more functions. This also makes it easier to pinpoint bugs, fix them without introducing more bugs, and helps prevent violation of SRP and creation of megamoths.
Using single-letter variable names is bad, and there is absolutely no reason to use them as we aren't limited on name length anymore. Looking through your code, I still don't know what
a, b, i, j, m, and the like mean, what they are, or what they do. Using descriptive names would tell me what they do so I could understand the code even if I didn't know JavaScript. Also, if you use good names, you won't need to give a comment stating what the variable is, does, or contains. Good code doesn't need a whole lot of comments. In fact, most of the comments I see are like this:catch
{
// gotcha!
}Sorting
var l = [ a[j].id, b[k].id ].smartSort('a');
var f = [ a.tagName, b.tagName ].smartSort('d');I assume
'a' and 'd' are specifying which way to sort the collection of items. You should avoid passing text values to specify what your program should do. In this case, I would pass a Boolean value, but you can define enums in JavaScript too.Functions
You don't need to all related code in one function. You should split your code up into more functions. This also makes it easier to pinpoint bugs, fix them without introducing more bugs, and helps prevent violation of SRP and creation of megamoths.
Code Snippets
catch
{
// gotcha!
}var l = [ a[j].id, b[k].id ].smartSort('a');
var f = [ a.tagName, b.tagName ].smartSort('d');Context
StackExchange Code Review Q#85700, answer score: 7
Revisions (0)
No revisions yet.