cleversort

A simple macro helper to make sorting arrays much prettier: array.cleverSort( _.age, _.name.toLowerCase() ).
https://github.com/jasononeil/cleversort

To install, run:

haxelib install cleversort 1.0.3 

See using Haxelib in Haxelib documentation for more information.

README.md

CleverSort

A very simple Haxe macro for helping to sort arrays based on multiple properties with a very simple syntax.

Usage

Imagine we have this data:

class Person {
	var firstName:String;
	var surname:String;
	var age:Int;
	var postcode:PostCode;
}
class PostCode {
	var state:StateEnum;
	var number:Int;
}
var array:Array<Person> = ...;

Include this at the top of your file, so that we can use the cleverSort() function on any array:

using CleverSort;

Example 1:

Sort by surname, firstName, age:

array.cleverSort(
	_.firstName.toLowerCase(),
	_.surname.toLowerCase(),
	_.age
);

// Which is the equivalent of writing

array.sort( function(i1,i2) {
	var cmp:Int;
	cmp = Reflect.compare(i1.firstName.toLowerCase(),i2.firstName.toLowerCase());
		if ( cmp!=0 ) return cmp;
	cmp = Reflect.compare(i1.surname.toLowerCase(),i2.surname.toLowerCase());
		if ( cmp!=0 ) return cmp;
	cmp = Reflect.compare(i1.age,i2.age);
		if ( cmp!=0 ) return cmp;
	return 0;
});

Example 2:

Sort by state, postcode, surname, firstname:

array.cleverSort(
	Type.enumConstructor( _.postcode.state ),
	_.postcode.number,
	_.surname,
	_.firstName
);

// Which is the equivalent of writing

array.sort( function(i1,i2) {
	var cmp:Int;
	cmp = Reflect.compare(Type.enumConstructor(i1.postcode.state),Type.enumConstructor(i2.postcode.state));
		if ( cmp!=0 ) return cmp;
	cmp = Reflect.compare(i1.postcode.number,i2.postcode.number);
		if ( cmp!=0 ) return cmp;
	cmp = Reflect.compare(i1.surname,i2.surname);
		if ( cmp!=0 ) return cmp;
	cmp = Reflect.compare(i1.firstName,i2.firstName);
		if ( cmp!=0 ) return cmp;
	return 0;
});

As you can see, the CleverSort syntax is much cleaner and more readable. Enjoy!

Code released into the Public Domain.

Contributors
jason
Version
1.0.3
Published
3 years ago
License
Public

All libraries are free

Every month, more than thousand developers use haxelib to find, share, and reuse code — and assemble it in powerful new ways. Enjoy Haxe; It is great!

Explore Haxe

Haxe Manual

Haxe Code Cookbook

Haxe API documentation

You can try Haxe in the browser! try.haxe.org

Join us on Github!

Haxe is being developed on GitHub. Feel free to contribute or report issues to our projects.

Haxe on Github