0
var array1 = ["ddd","aaa","eee","aaa","fff","bbb","ggg","aaa","ccc","fff"]

i have to arrange it in a way that identical values has to be placed together

output array should be

["ddd","aaa","aaa","aaa","eee","fff","fff","bbb","ggg","ccc"]

how to write the logic for this one ??

ani
  • 446
  • 1
  • 9
  • 31
  • 2
    Just sort it...? – Andrew Li Mar 30 '17 at 14:02
  • 1
    1) What have you tried? 2) Does the order matter? Could you have the `"aaa"` results in the front? – Mike Cluck Mar 30 '17 at 14:02
  • 1
    We're not here to write the code for you. Try to approach the problem, and if you get stuck, post your attempted solution along with what's going wrong. – T.J. Crowder Mar 30 '17 at 14:02
  • @crowder I tried on my own when I found it difficult I posted this code . first I tried to seperare the repeated element and then to merge it with old in a new array . here the array is going to be array of customer objects for(var i=0 ; i – ani Mar 31 '17 at 01:47
  • @crowder I thought creating two arrays and iterating this many times will affect the performance that's y I posted the question – ani Mar 31 '17 at 01:55

4 Answers4

1

You could iterate the array and check the position and if not the same as the actual element, splice the value to the earlier position.

var array = ["ddd","aaa","eee","aaa","fff","bbb","ggg","aaa","ccc","fff"],
    i = 0, p;
    
while (i < array.length) {
    p = array.indexOf(array[i]);
    if (p !== i) {
        array.splice(p, 0, array.splice(i, 1)[0]);
    }
    i++;
}

console.log(array);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
0

Sort array using sort() method like this

var array1 = ["ddd","aaa","eee","aaa","fff","bbb","ggg","aaa","ccc","fff"];

console.log(array1.sort());
Dhiraj
  • 1,430
  • 11
  • 21
0

Well actually for you request .sort() might be an overkill. You can simply do this by;

function quasiSort(a){
  var h = a.reduce((h,e) => h[e] ? (h[e].push(e), h)
                                 : (h[e] = [e], h), {});
  return Object.keys(h).reduce((r,k) => r.concat(h[k]),[]);
}
var array1 = ["ddd","aaa","eee","aaa","fff","bbb","ggg","aaa","ccc","fff"];
console.log(quasiSort(array1));
Redu
  • 25,060
  • 6
  • 56
  • 76
  • This works like a charm .. This function is very new to me . can you please explain how its working if you don't @redu – ani Mar 30 '17 at 17:16
  • @ani Happy that you have sorted out. Just let me know if you have any further questions. – Redu Mar 30 '17 at 17:43
  • can please explain how this function is working.. Hope it will definitely help others also .. – ani Mar 31 '17 at 01:10
  • can you please tell me how to use this method for grouping array of objects (e.g) var friends = [{ name: 'Anna', books: ['Bible', 'Harry Potter'], age: 21 }, { name: 'Bob', books: ['War and peace', 'Romeo and Juliet'], age: 26 }, { name: 'Alice', books: ['The Lord of the Rings', 'The Shining'], age: 18 },name: 'Anna', books: ['Bible', 'Alice in wonderland'], age: 23]; – ani Mar 31 '17 at 07:47
  • @ani It can be done but according to which property of the object you would like to group.. `name`? – Redu Mar 31 '17 at 08:42
  • 1
    @ani Please check [this](http://stackoverflow.com/a/43139954/4543207) up. – Redu Mar 31 '17 at 11:50
0

You can create one object to store values and index and then use that object to create new array.

var array1 = ["ddd","aaa","eee","aaa","fff","bbb","ggg","aaa","ccc","fff"],
 obj = {}, i = 0

array1.forEach(function(e) {
  if(!obj[e]) obj[e] = {index: i++, values: []}
  obj[e].values.push(e)
})

var result = [].concat(...Object.keys(obj).reduce((r, e) => (r[obj[e].index] = obj[e].values, r), []))

console.log(result)
Nenad Vracar
  • 118,580
  • 15
  • 151
  • 176
  • can you please explain your answer. I just want to understand how this reduce () is working – ani Mar 30 '17 at 17:32