--- warning: This file is auto generated by `yarn web:inject`, do not edit by hand examples: - compare('abc', 'abc') - compare('abc', 'abd') - compare('abd', 'abc') returns: - '0' - '-1' - '1' dependencies: [] authors: original by: - Kevin van Zonneveld (https://kvz.io) notes: [] parityVerified: - Tcl 8.6 isTypescript: true type: function layout: function title: Tcl's string compare in TypeScript description: >- Here's what our current TypeScript equivalent to Tcl's string compare looks like. function: compare category: string language: tcl permalink: tcl/string/compare/ alias: - /functions/tcl/compare/ - /functions/string/compare/ - /tcl/compare/ ---
{% codeblock lang:typescript %}export function compare(str1: string, str2: string): number { // discuss at: https://locutus.io/tcl/compare/ // parity verified: Tcl 8.6 // original by: Kevin van Zonneveld (https://kvz.io) // example 1: compare('abc', 'abc') // returns 1: 0 // example 2: compare('abc', 'abd') // returns 2: -1 // example 3: compare('abd', 'abc') // returns 3: 1 const left = String(str1) const right = String(str2) if (left === right) { return 0 } return left < right ? -1 : 1 } {% endcodeblock %}