Nodejs Array Compare compareLexicographically(arr)

Here you can find the source of compareLexicographically(arr)

Method Source Code

// Write a script that compares two char arrays
// lexicographically (letter by letter).

Array.prototype.compareLexicographically = function (arr){
    for (var ind = 0; ind < Math.min(this.length, arr.length); ind++) {
        if (arr[ind] !== this[ind]) {
            return this[ind] < arr[ind] ? -1 : 1;
        }/*w  ww. jav a 2s  .  c  o  m*/
    }

    if (this.length != arr.length) {
        this.length < arr.length ? -1 : 1;
    }

    return 0;
}

console.log(['a', 'b', 'c'].compareLexicographically(['a', 'b', 'c']));
console.log(['a', 'b', 'c'].compareLexicographically(['a', 'c', 'b']));
console.log(['a', 'c', 'b'].compareLexicographically(['a', 'b', 'c']));

Related

  1. compare(testArr)
    Array.prototype.compare = function(testArr) {
      if (this.length != testArr.length) return false;
      for (var i = 0; i < testArr.length; i++) {
        if (this[i].compare) { 
          if (!this[i].compare(testArr[i])) return false;
        };
        if (this[i] !== testArr[i]) return false;
      };
      return true;
    ...
    
  2. compareArray(array1)
    Array.prototype.compareArray = function(array1) {
      var array1 = array1;
      var array2 = this;
      if (array1.length === array2.length) {
        for (var i = 0; i < array1.length; i++) {
          for(var c = 0; c < array2.length; i++) {
            if (array1[i] === array2[c]) {
              return true;
      } else {
        return false;
      };
    
  3. compareArrays(arr)
    Array.prototype.compareArrays = function(arr) {
        if (this.length != arr.length) return false;
        for (var i = 0; i < arr.length; i++) {
            if (this[i].compareArrays) { 
                if (!this[i].compareArrays(arr[i])) return false;
                else continue;
            if (this[i] != arr[i]) return false;
        return true;
    };
    beforeEach(function() {
      this.addMatchers({
        toArrayEq: function(other){
          var that = this.actual;
          return that.compareArrays(other);  
      })
    });
    
  4. compareArrays(arr)
    Array.prototype.compareArrays = function(arr) {
        if (this.length != arr.length) return false;
        for (var i = 0; i < arr.length; i++) {
            if (this[i].compareArrays) { 
                if (!this[i].compareArrays(arr[i])) return false;
                else continue;
            if (this[i] != arr[i]) return false;
        return true;
    
  5. compareLexicographically(arr)
    Array.prototype.compareLexicographically = function (arr){
        for(var ind = 0; ind < Math.min(this.length, arr.length); ind++){
            if(arr[ind] !== this[ind]){
                return this[ind] < arr[ind] ? -1 : 1;
        if(this.length != arr.length){
            this.length < arr.length ? -1 : 1;
        return 0;
    var a = 'abc'.split(''), b = 'acb'.split('');
    console.log(a.compareLexicographically(b));