compare Summary Compares the String object to another String object. Usage public final Comparison compare(String object) Returns A System.Comparison enumeration result representing whether the specified String comparison object is less than, equal to, or greater than the current String object. Parameters object The String object to compare to. Description Compares the String object to another String object. Returns one of the following System.Comparison enumeration values: System.Comparison.LESS_THAN System.Comparison.EQUAL System.Comparison.GREATER_THAN Examples Basic Usage 12345import System; string s = "abc";Comparison result = s.compare("abc");Console.log(result == Comparison.EQUAL); Sorting 12345import System; string[] arr = [ "Joe", "Frank", "Mary", "John" ];arr.sort(); // 'System.Array<T>.sort' uses 'compare' to sort elementsConsole.log(arr.join(", ")); Custom Sort 1234567891011121314151617import System;import System.Assert; string[] arr = [ "Joe", "Frank", "Mary", "John" ];arr.sort(Comparison(string a, string b) { if (a < b) { return Comparison.LESS_THAN; } else if (a > b) { return Comparison.GREATER_THAN; } else { assert(a == b); return Comparison.EQUAL; }});Console.log(arr.join(", ")); Share HTML | BBCode | Direct Link