Sugar icon indicating copy to clipboard operation
Sugar copied to clipboard

Is there something for sorting arrays by multiple items?

Open Costegillio opened this issue 7 years ago • 3 comments

Greetings!

I can't find any info in official (docs) for sorting multidimensional arrays by multiple items with choosing sorting direction.

So, i wrote this function: ` window.sortByMultiple = function(arr, field1, direction1, field2, direction2){

	arr.sort(function (a, b) {

		var field1A = a[field1];
		var field1B = b[field1];

		var field2A = a[field2];
		var field2B = b[field2];

		if (field1A === field1B) {
		
			if (direction2 == "down") {
				return (field2A > field2B) ? -1 : (field2A < field2B) ? 1 : 0;
			} else {
				return (field2A < field2B) ? -1 : (field2A > field2B) ? 1 : 0;
			}
		
		} else {
			
			if (direction1 == "down") {
				return (field1A > field1B) ? -1 : (field1A < field1B) ? 1 : 0; 
			} else {
				return (field1A < field1B) ? -1 : (field1A > field1B) ? 1 : 0;
			}
		
		}

	});

}

` If it's could be usefull, feel free to use it in sugar. If sugar already has something like that, please help me to find it.

Costegillio avatar Jun 18 '17 09:06 Costegillio

This looks like a good candidate for a plugin. Have a look at the readme here.

andrewplummer avatar Jun 18 '17 09:06 andrewplummer

Sorry, i haven't time for it. Anyway, thanks for your project!

Costegillio avatar Jun 21 '17 18:06 Costegillio

Will give this some consideration for the next version. Thanks

andrewplummer avatar Aug 06 '18 13:08 andrewplummer