Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

170
Views
How to write a merge sort with JavaScript in Vue.Js

I am try to do a simple merge sort with JavaScript in Vue.Js. These are the functions I created. But the output can't sort the array ascendingly.

    mergeSort(arr) {
      if (arr.length <= 1) {
        this.data.arrNum = arr;
      }
      console.log("arr", arr);
      let mid = Math.floor(arr.length / 2);
      let left = arr.slice(0, mid);
      console.log("left", left);
      let right = arr.slice(mid);
      console.log("right", right);

      this.merge(left, right);
    },

    merge: function (left, right) {
      var leftIndex = 0;
      var rightIndex = 0;
      var results = [];

      while (leftIndex < left.length && rightIndex < right.length) {

        if (left[leftIndex] < right[rightIndex]) {
          results.push(left[leftIndex]);
          leftIndex++    
        } else {
          results.push(right[rightIndex]);
          rightIndex++; 
        }
      }

      results = results
        .concat(left.slice(leftIndex))
        .concat(right.slice(rightIndex));
      this.data.arrNum = results;
    },
  }
};

Sample Original Array Before sorted Output After sorted

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!