Skip to content

Instantly share code, notes, and snippets.

@rogerpoon
Created October 29, 2017 05:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rogerpoon/f4afbe455956b638b53f839de2502560 to your computer and use it in GitHub Desktop.
Save rogerpoon/f4afbe455956b638b53f839de2502560 to your computer and use it in GitHub Desktop.
JS++ Custom Sorting
import System;
class Employee : IComparable<Employee>
{
private string firstName;
private string lastName;
public Employee(string firstName, string lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public Comparison compare(Employee that) {
// Sort by employee surname
return this.lastName.compare(that.lastName);
}
public override string toString() {
return this.firstName + " " + this.lastName;
}
}
Employee zig = new Employee("Zig", "Ziglar");
Employee john = new Employee("John", "Smith");
Employee abe = new Employee("Abe", "Lincoln");
Employee[] employees = [ zig, john, abe ];
employees.sort();
Console.log(employees.join(", "));
// Output:
// Abe Lincoln, John Smith, Zig Ziglar
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment