How to sort objects by date ascending order? - javascript

If I have a list of object:
var objectList= LIST_OF_OBJECT;
each object in the list contains three attributes: "name", "date","gender"
How to sort the objects in the list by "date" attribute ascending order?
(the "date" attribute contain string value like "2002-08-29 21:15:31+0500")

If your objects have the date information within a String field:
yourArray.sort(function(a, b) { return new Date(a.date) - new Date(b.date) })
or, if they have it within a Date field:
yourArray.sort(function(a, b) { return a.date - b.date })

The Array.sort method accepts a sort function, which accepts two elements as arguments, and should return:
< 0 if the first is less than the second
0 if the first is equal to the second
> 0 if the first is greater than the second.
.
objectList.sort(function (a, b) {
var key1 = a.date;
var key2 = b.date;
if (key1 < key2) {
return -1;
} else if (key1 == key2) {
return 0;
} else {
return 1;
}
});
You're lucky that, in the date format you've provided, a date that is before another date is also < than the date when using string comparisons. If this wasn't the case, you'd have to convert the string to a date first:
objectList.sort(function (a, b) {
var key1 = new Date(a.date);
var key2 = new Date(b.date);
if (key1 < key2) {
return -1;
} else if (key1 == key2) {
return 0;
} else {
return 1;
}
});

yourArray.sort(function(a, b) {
a = new Date(a.date);
b = new Date(b.date);
return a >b ? -1 : a < b ? 1 : 0;
})

You can try:
<script src="https://cyberknight.000webhostapp.com/arrange.js">
var a =[-1,0,5,4,3,2,6,1,1];
var b = totzarrange(a)
console.log(b);
</script>

Related

d3 js sort error in chrome [duplicate]

I have an array of objects to sort. Each object has two parameters: Strength and Name
objects = []
object[0] = {strength: 3, name: "Leo"}
object[1] = {strength: 3, name: "Mike"}
I want to sort first by Strength and then by name alphabetically. I am using the following code to sort by the first parameter. How do I sort then by the second?
function sortF(ob1,ob2) {
if (ob1.strength > ob2.strength) {return 1}
else if (ob1.strength < ob2.strength){return -1}
return 0;
};
Thanks for your help.
(I am using Array.sort() with the aforementioned sortF as the sort comparison function passed into it.)
Expand your sort function to be like this;
function sortF(ob1,ob2) {
if (ob1.strength > ob2.strength) {
return 1;
} else if (ob1.strength < ob2.strength) {
return -1;
}
// Else go to the 2nd item
if (ob1.name < ob2.name) {
return -1;
} else if (ob1.name > ob2.name) {
return 1
} else { // nothing to split them
return 0;
}
}
A < and > comparison on strings is an alphabetic comparison.
This little function is often handy when sorting by multiple keys:
cmp = function(a, b) {
if (a > b) return +1;
if (a < b) return -1;
return 0;
}
or, more concisely,
cmp = (a, b) => (a > b) - (a < b)
Which works because in javascript:
true - true // gives 0
false - false // gives 0
true - false // gives 1
false - true // gives -1
Apply it like this:
array.sort(function(a, b) {
return cmp(a.strength,b.strength) || cmp(a.name,b.name)
})
Javascript is really missing Ruby's spaceship operator, which makes such comparisons extremely elegant.
You could chain the sort order with logical OR.
objects.sort(function (a, b) {
return a.strength - b.strength || a.name.localeCompare(b.name);
});
When I was looking for an answer to this very question, the answers I found on StackOverflow weren't really what I hoped for. So I created a simple, reusable function that does exactly this. It allows you to use the standard Array.sort, but with firstBy().thenBy().thenBy() style.
https://github.com/Teun/thenBy.js
PS. This is the second time I post this. The first time was removed by a moderator saying "Please don't make promotional posts for your own work". I'm not sure what the rules are here, but I was trying to answer this question. I'm very sorry that it is my own work. Feel free to remove again, but please point me to the rule involved then.
steve's answer, but prettier.
objects.sort(function(a,b)
{
if(a.strength > b.strength) {return 1;}
if(a.strength < b.strength) {return -1;}
if(a.name > b.name ) {return 1;}
if(a.name < b.name ) {return -1;}
return 0;
}
function sortF(ob1,ob2) {
if (ob1.strength > ob2.strength) {return 1}
else if (ob1.strength < ob2.strength) {return -1}
else if (ob1.name > ob2.name) {return 1}
return -1;
};
EDIT: Sort by strength, then if strength is equal, sort by name.
The case where strength and name are equal in both objects doesn't need to be accounted for seperately, since the final return of -1 indicates a less-than-or-equal-to relationship. The outcome of the sort will be correct. It might make it run faster or slower, I don't know. If you want to be explicit, just replace
return -1;
with
else if (ob1.name < ob2.name) {return -1}
return 0;
Find 'sortFn' function below. This function sorts by unlimited number of parameters(such as in c#: SortBy(...).ThenBy(...).ThenByDesc(...)).
function sortFn() {
var sortByProps = Array.prototype.slice.call(arguments),
cmpFn = function(left, right, sortOrder) {
var sortMultiplier = sortOrder === "asc" ? 1 : -1;
if (left > right) {
return +1 * sortMultiplier;
}
if (left < right) {
return -1 * sortMultiplier;
}
return 0;
};
return function(sortLeft, sortRight) {
// get value from object by complex key
var getValueByStr = function(obj, path) {
var i, len;
//prepare keys
path = path.replace('[', '.');
path = path.replace(']', '');
path = path.split('.');
len = path.length;
for (i = 0; i < len; i++) {
if (!obj || typeof obj !== 'object') {
return obj;
}
obj = obj[path[i]];
}
return obj;
};
return sortByProps.map(function(property) {
return cmpFn(getValueByStr(sortLeft, property.prop), getValueByStr(sortRight, property.prop), property.sortOrder);
}).reduceRight(function(left, right) {
return right || left;
});
};
}
var arr = [{
name: 'marry',
LocalizedData: {
'en-US': {
Value: 10000
}
}
}, {
name: 'larry',
LocalizedData: {
'en-US': {
Value: 2
}
}
}, {
name: 'marry',
LocalizedData: {
'en-US': {
Value: 100
}
}
}, {
name: 'larry',
LocalizedData: {
'en-US': {
Value: 1
}
}
}];
document.getElementsByTagName('pre')[0].innerText = JSON.stringify(arr)
arr.sort(sortFn({
prop: "name",
sortOrder: "asc"
}, {
prop: "LocalizedData[en-US].Value",
sortOrder: "desc"
}));
document.getElementsByTagName('pre')[1].innerText = JSON.stringify(arr)
pre {
font-family: "Courier New" Courier monospace;
white-space: pre-wrap;
}
Before:
<pre></pre>
Result:
<pre></pre>
With ES6 you can do
array.sort(function(a, b) {
return SortFn(a.strength,b.strength) || SortFn(a.name,b.name)
})
private sortFn(a, b): number {
return a === b ? 0 : a < b ? -1 : 1;
}
Here is the function I use. It will do an arbitrary number.
function Sorter(){
var self = this;
this.sortDefs = [];
for (let i = 0; i < arguments.length; i++) {
// Runs 5 times, with values of step 0 through 4.
this.sortDefs.push(arguments[i]);
}
this.sort = function(a, b){
for (let i = 0; i < self.sortDefs.length; i++) {
if (a[self.sortDefs[i]] < b[self.sortDefs[i]]) {
return -1;
} else if (a[self.sortDefs[i]] > b[self.sortDefs[i]]) {
return 1
}
}
return 0;
}
}
data.sort(new Sorter('category','name').sort);
In 2018 you can use just sort() ES6 function, that do exactly, what you want.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort

How to get desired sorting output in an array in javascript

I am trying to sort the object inside an array below in ascending order by it value and get the desired output as below:-
var arr = [{"DOA Qty":"0.000665921017598927382910198160","LOS%":"0","FID Valid EC By Part":"0.0041860443283016713761966","Interventions":"0"}]
Desired output - sort in ascending order by value:
var desiredarr =[{"LOS%":"0","Interventions":"0","DOA Qty":"0.000665921017598927382910198160","FID Valid EC By Part":"0.0041860443283016713761966"}]
let sorteddataarr: any = Object.values(arr[0]).sort(function (a, b) { return arr[a] - arr[b]; });
alert(JSON.stringify(sorteddataarr)); // not giving result
a[1]-b[1] == :ASEC
b[1]-a[1] == :DESC
Try this :
var obj =
{
"DOA Qty":"0.000665921017598927382910198160",
"LOS%":"0",
"FID Valid EC By Part":"0.0041860443283016713761966",
"Interventions":"0"
}
var entries = Object.entries(obj)
entries.sort(function(a,b){return a[1]-b[1]});
obj = {};
entries.map(function(item){
obj[item[0]] = item[1];
})
console.log(obj);
Maybe the following will help, the keys of an object are given in no particular order when doing things like Object.keys or for something in ..., since the order is important to you I converted the object(s) to array(s) of key(s) and value(s):
var arr = [{"DOA Qty":"0.000665921017598927382910198160","LOS%":"0","FID Valid EC By Part":"0.0041860443283016713761966","Interventions":"0"}];
console.log(
arr.map(
object=>
Object.keys(object).map(
key=>[Number(object[key]),key]//your values are not numbers, maybe create better JSON
).sort(
([a],[b])=>a-b
)
//optional, if you rather have [key,value] .map(([value,key])=>[key,value]
)
)
Create a simple compare function.
Define the key to use in the function. I used value
Then call Array.prototype.sort() and pass in your compare function
The only difference between the two for DESC or ASC order is that the greater than and less than symbols are switched
function compareDESC(a, b) {
if (a.value < b.value)
return 1;
if (a.value > b.value)
return -1;
return 0;
}
function compareASC(a, b) {
if (a.value > b.value)
return 1;
if (a.value < b.value)
return -1;
return 0;
}
var arr = [
{
value: 2
},
{
value: 6
},
{
value: 3
},
{
value: 8
},
{
value: 9
},
{
value: 4
},
];
arr.sort(compareDESC)
console.log(arr)
arr.sort(compareASC)
console.log(arr)

Sort an Array of Objects based on key, and based on value

Consider this:
[{name:'John'},{age:25},{address:'some street'}]
As you can see none of the keys are a consistent name, so I cannot use.
arr.sort((a,b)=> a.consistentKey < b.consistentKey);
How can I go about sorting something like this, by name, and by value?
so the following sorted by key in alphabetical order should be:
[{address:'some street'},{age:25},{name:'John'}]
If you are thinking of sorting on the basis of key first and then further on values, you can try the following :
var a = [{name:'John'},{age:25},{address:'some street'}];
alert(JSON.stringify(a.sort((a, b) => {
nameA = Object.keys(a)[0];
nameB = Object.keys(b)[0];
if (nameA < nameB) {
return -1;
}
if (nameA > nameB) {
return 1;
}
// names must be equal
return 0;
})));
Here I have considered only one key, but you can always extend it to multiple keys and similarly you can further sort on the basis of values too.
If you extract the key name using Object.keys, then you can get the values you need to perform the comparison:
[{name: 'John'}, {age: 25}, {address:'some street'}].sort((a, b) => {
const keyA = Object.keys(a)[0]
const valA = a[keyA]
const keyB = Object.keys(b)[0]
const valB = a[keyB]
if (keyA > keyB) {
return 1
} else if (keyA < keyB) {
return -1
} else /* equal */ {
if (valA > valB) {
return 1
} else if (valA < valB) {
return -1
} else /* equal */ {
return 0
}
}
})
You can do this with
input.sort((a, b) => {
const keya = Object.keys(a)[0];
const keyb = Object.keys(b)[0];
return keya.localeCompare(keyb) || a[keya].localeCompare(b[keyb]);
});
Using localeCompare is both shorter and more robust in the face of different language locales.
Below is the solution I would use. This solution provides a keys only sort, a values only sort, a keys then values sort, and a values then keys sort.
class FunkySort {
sort (sortType) {
switch (sortType) {
case 'keysOnly':
return data => this._sortByKey(data);
case 'valuesOnly':
return data => this._sortByValue(data);
case 'valuesPrimary':
return data => {
data = this._sortByKey(data);
return this._sortByValue(data);
};
case 'keysPrimary':
return data => {
data = this._sortByValue(data);
return this._sortByKey(data);
};
}
}
_sortByKey (data) {
return data.sort((a, b) => {
var keyA = Object.keys(a)[0];
var keyB = Object.keys(b)[0];
return keyA < keyB ? -1 : keyA > keyB ? 1 : 0;
});
}
_sortByValue (data) {
return data.sort((a, b) => {
// note that in Node >=v7 you could use `Object.values()`, but not in <v7.0
var valueA = a[Object.keys(a)[0]];
var valueB = b[Object.keys(b)[0]];
return valueA < valueB ? -1 : valueA > valueB ? 1 : 0;
});
}
}
const dataArr = [{name:'John'},{age:25},{address:'some street'}];
const fs = new FunkySort();
fs.sort('keysPrimary')(dataArr);
Note that fs.sort is a curried function. The first call sets the type of sort to be done, so fs.sort('keysPrimary') returns a function that takes an array of objects and sorts it first by the values, and then by the keys, resulting in an array of objects sorted by key, and if there are multiple objects with the same key, those are sorted by value.
If you don't need this level of flexibility in the type of sort, then just the _sortByKey helper method should suffice.

How to sort array of strings based on a contained number?

I have an array with strings like this:
"1115.49|Onroll|Paiporta|/v2/networks/onroll-paiporta"
"1767.92|Göteborg|Göteborg|/v2/networks/goeteborg"
"190.4|ARbike|Arezzo|/v2/networks/arbike"
"201.36|JesinBici|Jesi|/v2/networks/jesinbici"
"403.59|Venezia|Venezia|/v2/networks/venezia"
"395.07|Mantova|Mantova|/v2/networks/mantova"
the first value is a distance, I would like to sort the array based on that distance, how can I do?
Everything I've tried does not work, I would that 1000 come after 200 not before!
thanks!
You can do something like this:
yourArray.sort(function (a, b) {
var aNum = +a.substring(0, a.indexOf('|'));
var bNum = +b.substring(0, b.indexOf('|'));
if (aNum > bNum) return 1;
if (aNum < bNum) return -1;
return 0;
});
which will return an array in the ascending order you wanted.
If you add a sortBy function to Array.prototype you can do things like that more easily.
Array.prototype.sortBy = function(f) {
this.sort(function(a, b) {
a = f(a); b = f(b);
if(a > b) return 1;
if(a < b) return -1;
return 0;
});
}
and then you can write
array.sortBy(function(s) { return +s.split("|")[0]; });

Sort array on key value

I have a function which sorts by name currently and an array of value / key pairs.
I wonder how can I pass the key on which sort is being performed so I can call the same function every time like so:
var arr = [{name:'bob', artist:'rudy'},
{name:'johhny', artist:'drusko'},
{name:'tiff', artist:'needell'},
{name:'top', artist:'gear'}];
sort(arr, 'name'); //trying to sort by name
sort(arr, 'artist'); //trying to sort by artist
function sort(arr) {
arr.sort(function(a, b) {
var nameA=a.name.toLowerCase(), nameB=b.name.toLowerCase();
if (nameA < nameB) //sort string ascending
return -1;
if (nameA > nameB)
return 1;
return 0; //default return value (no sorting)
});
}
Array.prototype.sortOn = function(key){
this.sort(function(a, b){
if(a[key] < b[key]){
return -1;
}else if(a[key] > b[key]){
return 1;
}
return 0;
});
}
var arr = [{name:'bob', artist:'rudy'},{name:'johhny', artist:'drusko'},{name:'tiff', artist:'needell'},{name:'top', artist:'gear'}];
arr.sortOn("name");
arr.sortOn("artist");
[edit 2020/08/14] This was rather an old answer and not very good as well, so simplified and revised.
Create a function that returns the sorting lambda (the Array.prototype.sort callback that does the actual sorting). That function can receive the key name, the kind of sorting (string (case sensitive or not) or numeric) and the sorting order (ascending/descending). The lambda uses the parameter values (closure) to determine how to sort.
const log = (...strs) =>
document.querySelector("pre").textContent += `\n${strs.join("\n")}`;
const showSortedValues = (arr, key) =>
` => ${arr.reduce((acc, val) => ([...acc, val[key]]), [])}`;
// the actual sort lamda factory function
const sortOnKey = (key, string, desc) => {
const caseInsensitive = string && string === "CI";
return (a, b) => {
a = caseInsensitive ? a[key].toLowerCase() : a[key];
b = caseInsensitive ? b[key].toLowerCase() : b[key];
if (string) {
return desc ? b.localeCompare(a) : a.localeCompare(b);
}
return desc ? b - a : a - b;
}
};
// a few examples
const onNameStringAscendingCaseSensitive =
getTestArray().sort( sortOnKey("name", true) );
const onNameStringAscendingCaseInsensitive =
getTestArray().sort( sortOnKey("name", "CI", true) );
const onValueNumericDescending =
getTestArray().sort( sortOnKey("value", false, true) );
// examples
log(`*key = name, string ascending case sensitive`,
showSortedValues(onNameStringAscendingCaseSensitive, "name")
);
log(`\n*key = name, string descending case insensitive`,
showSortedValues(onNameStringAscendingCaseInsensitive, "name")
);
log(`\n*key = value, numeric desc`,
showSortedValues(onValueNumericDescending, "value")
);
function getTestArray() {
return [{
name: 'Bob',
artist: 'Rudy',
value: 23,
}, {
name: 'John',
artist: 'Drusko',
value: 123,
}, {
name: 'Tiff',
artist: 'Needell',
value: 1123,
}, {
name: 'Top',
artist: 'Gear',
value: 11123,
}, {
name: 'john',
artist: 'Johanson',
value: 12,
}, ];
}
<pre></pre>
function keysrt(key) {
return function(a,b){
if (a[key] > b[key]) return 1;
if (a[key] < b[key]) return -1;
return 0;
}
}
someArrayOfObjects.sort(keysrt('text'));
Make your life easy and use a closure
https://stackoverflow.com/a/31846142/1001405
You can see the working example here
var filter = 'name', //sort by name
data = [{name:'bob', artist:'rudy'},{name:'johhny', artist:'drusko'},{name:'tiff', artist:'needell'},{name:'top', artist:'gear'}];;
var compare = function (filter) {
return function (a,b) { //closure
var a = a[filter],
b = b[filter];
if (a < b) {
return -1;
}else if (a > b) {
return 1;
} else {
return 0;
}
};
};
filter = compare(filter); //set filter
console.log(data.sort(filter));
Looking at all the answers, I came up with my own solution that works cross-browser. The accepted solution does not work in IE or Safari. Also, the other solutions do not allow for sorting by descending.
/*! FUNCTION: ARRAY.KEYSORT(); **/
Array.prototype.keySort = function(key, desc){
this.sort(function(a, b) {
var result = desc ? (a[key] < b[key]) : (a[key] > b[key]);
return result ? 1 : -1;
});
return this;
}
var arr = [{name:'bob', artist:'rudy'}, {name:'johhny', artist:'drusko'}, {name:'tiff', artist:'needell'}, {name:'top', artist:'gear'}];
arr.keySort('artist');
arr.keySort('artist', true);

Categories

Resources