0

If You want to sort Arr1 according to its element's object property Id value, then placing the arranged array Arr1 in Arr2 so finally Arr2 will be:

var Arr2 = [{Id:1,Name:'Ajay1'},{Id:3,Name:'Ajay3'},{Id:5,Name:'Ajay5'},{Id:2,Name:'Ajay2'},{Id:4,Name:'Ajay4'}];
5
  • 1) This isn't a question and 2) your answer isn't working. Commented Dec 7, 2014 at 18:43
  • @Robert If you checked "Ask Question" You will find under the post button a check box labeled "Answer your own question – share your knowledge, Q&A-style " I did this for that purpose. Commented Dec 7, 2014 at 21:33
  • Sorry, but I think you still need to ask a question in order to answer it. Anyways, I don't even see how this is sorting anything since the result seems to be ordered randomly. The "question" doesn't state what Arr1 and Arr2 look like for this result or what "placing Arr1 in Arr2" is supposed to mean. Actually I have no idea what you're trying to achieve. Commented Dec 7, 2014 at 23:38
  • Did you tested the demo? If yes, check your browser's settings, it may block alert Commented Dec 8, 2014 at 7:55
  • Yes, I tested the demo and it alerts the ids 1,3,5,2,4. I suppose the migration step is implemented incorrectly. But how should anyone know what's wrong with your answer if you refuse to post a proper question? If you look closely also ganbustein didn't address the "question" at all. Commented Dec 8, 2014 at 16:15

2 Answers 2

2

BubbleSort has absolutely nothing to recommend it except for its catchy name. It isn't the fastest, and it isn't even the easiest to write.

ExtractionSort and InsertionSort are both faster and easier to write. (All three sorts take time proportional to the square of the number of elements being sorted, but the constant of proportionality is smaller by a factor of at least two for the latter two sorts.)

Here's InsertionSort, defined to sort an array A in-place. (If you don't want to sort in-place, make a copy first and then sort the copy in-place.)

function InsertionSort(A) {
    var N = A.length;
    if (N < 2) { return; }

    // Simplify the main loop by first moving the smallest element to the front
    var leastInx = 0, leastID = A[0].Id;
    for (var i = 1; i < N; ++i) { // note: i++ is cuter, but ++i is often faster
        var thisID = A[i].Id;     // avoid doing the same work twice
        if (thisId < leastId]) {
            leastInx = i; leastId = thisId; }}
    var tmp = A[0];
    A[0] = A[leastInx];
    A[leastInx] = tmp;

    // Now iterate over the remaining elements, inserting each in its
    // proper place among the already-sorted previous elements
    for (i = 2; i < N; ++i) {
        tmp = A[i];
        for (var j = i; A[j-1].Id > tmp.Id; --j) {    // no need to test j>0
            A[j] = A[j-1]; }
        A[j] = tmp; }
    }

Of course, the built-in sorts will have running times on the order of N log(N), which will blow away all of these O(N**2) sorts. Writing your own sort routine can be instructive, and I commend it if for no other reason, but if you want speed avail yourself of the skill of the experts.

Sign up to request clarification or add additional context in comments.

Comments

0

We can use bubble sort algorithm to sort or arrange Arr1 then iterate over Arr2 to migrate arranged elements of Arr1 to Arr2. In the following solution we will not use any native methods for sorting, it is just looping and logical comparison.

 var Arr1 = [{Id:1,Name:'Ajay1'},{Id:5,Name:'Ajay5'},{Id:3,Name:'Ajay3'}];
var Arr2 = [{Id:3,Name:'Ajay3'},{Id:5,Name:'Ajay5'},{Id:1,Name:'Ajay1'},{Id:2,Name:'Ajay2'},{Id:4,Name:'Ajay4'}];
//var Arr1Res = [];   
    for (i = 0; i < Arr1.length; i++){      
      for (j = 0; j < (Arr1.length-1); j++){
        if (Arr1[j].Id > Arr1[j+1].Id){
           tmp = Arr1[j];
           Arr1[j] = Arr1[j + 1];
           Arr1[j + 1] = tmp;
        }
      }
    }
// Migrating Arranged Arr1 to Arr2    
    for (x = 0; x < Arr2.length; x++){
      for (y = 0; y < Arr1.length; y++){
        if (Arr1[y].Id != Arr2[x].Id){
          Arr2[y] = Arr1[y]
        }        
      }
    }
    Arr1Res = Arr2 // You can replace Arr2 by Arr1 to check Arranged Arr1

    for (k = 0; k < Arr1Res.length; k++){
      alert(Arr1Res[k].Id+"---"+Arr1Res[k].Name)
    }

Checkout the following DEMO:

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.