Skip to content

Commit

Permalink
Create sortBy.js
Browse files Browse the repository at this point in the history
  • Loading branch information
shysolocup authored Nov 17, 2023
1 parent bf4b181 commit b415fa9
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/Soup/functions/sortBy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const Soup = require('../index.js');


function SoupSortBy(obj, compare=null) {
if (!obj.type) obj = Soup.from(obj);
obj = obj.copy();
var copy = this.copy();

var stuff = [];

obj.sort( (rawA, rawB) => {
let a = (typeof rawA == "string") ? rawA.toUpperCase().localeCompare(rawB) : rawA;
let b = (typeof rawB == "string") ? rawB.toUpperCase().localeCompare(rawA) : rawB;

stuff.push( [a, b] );

return (compare) ? compare(a, b) : a - b;
});

var i = 0;

return new copy.constructor(copy.sort( (_a, _b) => {
let [a, b] = stuff[i]; i++;

return (compare) ? compare(a, b) : a - b;
}));
}


Soup.newF("sortBy", SoupSortBy);

0 comments on commit b415fa9

Please sign in to comment.