5

var points = [2, a, t, 5, 4, 3]; and after sort: var points = [2, a, t, 3, 4, 5];

Any help about that? it is possible to post your help with old style javascript code because i work on Adobe DC project?

my code is there :

var points = [t1, t2, t3, t4];
[t1, t2, t3, t4] = points;

var lucky = points.filter(function(number) {
  return isNaN(number) == false && number !=="" && number!== undefined;
});
points=lucky
points.sort(function(a, b){return a - b});

this.getField("Text6").value = points

but this only sort min to max and filter other characters... i need to keep other characters and short only numbers...

2
  • 1
    Welcome to SO. The idea is you show what you've tried, then we help. Commented Feb 14, 2019 at 11:22
  • without showing code, I could suggest to splice the array sort it separately and then combine it Commented Feb 14, 2019 at 11:24

4 Answers 4

3

var points = [2, "a", "t", 5, 4, 11, "", 3];
var insert = points.slice(0); // Clone points

// Remove all elements not a number
points = points.filter(function(element) {
  return typeof element === 'number';
});

// Sort the array with only numbers
points.sort(function(a, b) {
  return a - b;
});

// Re-insert all non-number elements
insert.forEach(function(element, index) {
  if (typeof element !== 'number') {
    points.splice(index, 0, element);
  }
});

console.log(points);

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

6 Comments

Nice. Actually this looks like a good interview question to use in the future given how many answerers (myself included) got this wrong
I agree @pete, it's deceptively simple.
Why used only sort() it will not work for greater numbers.
@Armel one bug i found is to short and "" values... and not keep is the table position how to fix this?
@MaheerAli You are right, I have added a compare function in the sort().
|
2

You may be able to do this more efficiently by avoiding splice() if you simply reassign the values in place.

Filter, sort, the reassign to sort the numbers in place:

let arr = ['c', 2, 'a', 't', 5, 4, 3, 'b', 1]

let n = arr.filter(c => typeof c == "number").sort((a, b) => a-b)
for (let i = 0, c=0; i<arr.length; i++){
    if (typeof arr[i] == "number") arr[i] = n[c++]
}
console.log(arr)

1 Comment

I like your condensed solution but I kept something a bit more verbose and less ES6. I guess it depends on what you are looking for ;) Thanks.
1

filter() numbers out of array. Then sort() them and then loop through real array. And check if value is Number change it will first element of sorted array. And remove first element of sortedArr

let arr = [5, 't', 's', 2,3,4];
let sortedNums = arr.filter(x => typeof x === 'number').sort((a,b) => a-b);
arr.forEach((a,i) => {
  if(typeof a === 'number'){
    arr[i] = sortedNums[0]
    sortedNums.shift();
  } 
})

console.log(arr)

1 Comment

@MarkMeyer 🤦‍♂️ True
0

The following should give the exact output given your input:

var result = [2, 'a', 't', 5, 4, 3].reduce(
  function(result,item){
    return !isNaN(item)&&item!==''
      ? typeof (result[result.length-1] && result[result.length-1].push) === 'function'
        ? (result[result.length-1].push(item),result)
        : result.concat([[item]])
      : result.concat(item)
  },
  []
).map(
  function(item){
    return typeof item.sort === 'function'
      ? item.sort(function(a,b){return a-b})
      : item
  }
).reduce(
  function(result,item){
    return result.concat(item)
  },[]
);

console.log(result);

2 Comments

The way I read the question was to sort all the numbers as a single group rather than sorting the subgroups of numbers. For example, the expected output of [2, 'a', 't', 5, 4, 3, 1] would be [1, 'a', 't', 2, 3, 4, 5]. The code above returns [ 2, 'a', 't', 1, 3, 4, 5 ].
@MarkMeyer could be the case as well

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.