 Level 1

0% to Level 2

Tip /

to gain points, level up, and earn exciting badges like the new
Mission!

View all

SOLVED

## Sort listBox that contains text and numbers  Level 9

Is there a script to sort a listBox that contains text and numbers using a button?

I would prefer it sorted 1, 2, 10, 20, 100, not 1, 10, 100, 2, 20

Examples items are part numbers: HP1457100-9, 011-2025-479, 85216, etc

1 Accepted Solution  Level 10

You may have to change this line:
oTarget = this,

It references the object to display the results.

20 Replies  Level 10

Hi there,

if you put all the values within an array you can sort the values to your liking using the sort method

If you want to compare a string value with numbers within, you will have to play a bit with algorithm to expect sorting them alphabetically by splitting the string and numbers and then compare both to tell which one goes first.

I hope this can help you understanding how to sort values, it might get tricky with the values you provided  Level 9

I'm having trouble with this. I appreciate your help. Here's my script:

var temp = new Array();
var i;
//Create a two-dimensional array to hold the PartsListBox (original unsorted list) list items
for(i=0; i < PartsListBox.length; i++){
temp[i] = new Array();
temp[i] = PartsListBox.getSaveItem(i);
temp[i] = PartsListBox.getDisplayItem(i);
}

temp.sort(mySortFunction);
function mySortFunction(a,b){
return a-b;
}
temp.sort(mySortFunction);
function mySortFunction(a,b){
return a.toString().toLocaleCompare(b.toString());
}

//Add the sorted array back into the ListBox
PartsListBox.clearItems();
for(i=0; i<temp.length; i++)
{
}

I get an error that return a.toString().toLocaleCompare(b.toString()); is not a function.  Level 10

Try this code:

`var oSource = PartsListBox,  aList = [],  sortValues = function(a, b) {  return a - b;  };for (var i = 0; i < oSource.length; i += 1){  aList.push([oSource.getSaveItem(i), oSource.getDisplayItem(i), i]);}aList.sort(sortValues);oSource.clearItems();aList.forEach(function (element) {  oSource.addItem(element, element);});`  Level 9

No, unfortunitly that code does not work. If I enter the following part numbers: and then click the Sort Parts List button containing the code, this is what I get:   Level 10

Ok,  let's try this one. It checks if the sorted values are numbers or not and will use a different methods.

`var oSource = PartsListBox,  oTarget = this,  aList = [],  sortValues = function(a, b) {  var isRealNaN = function (test) {  return test !== test;  },  testA = isRealNaN(parseFloat(a)),  testB = isRealNaN(parseFloat(b));   // A is NaN  if (testA === true) {  // B is NaN  if (testB === true) {  if (a < b) {  return -1;  }   if (a > b) {  return 1;  }  // B is not NaN  } else {  return 1;  }  // A is not NaN  } if (testA === false) {  // B is NaN  if (testB === true) {  return -1;  // B is not NaN  } else {  return a - b;  }  }  return 0;  };for (var i = 0; i < oSource.length; i += 1){  aList.push([oSource.getSaveItem(i), oSource.getDisplayItem(i), i]);}aList.sort(sortValues);oTarget.clearItems();aList.forEach(function (element) {  oTarget.addItem(element, element);});`  Level 9

First of all, I really appreciate your hep with this.

i added tthe script to the Sort button but cannot get it to run when clicking the button.  Level 10

If I understand well, this might do the trick then... not sure if it fits your needs but I surely hope so.  Level 10

You may have to change this line:
oTarget = this,

It references the object to display the results.  Level 9

Yes! That's what it needed. I changed "this" to PartsListBox and it works!

Thank you so much!  Level 9

Magus069,

Thank you for your help trying to solve this sort issue. It has been solved.  Level 9

The only problem I stilll have is part numbers that start with 0 like 015645 do not sort correctly.  Level 10

var myArray = new Array();

//Retrieve List Box's values

for (var i = 0; i < PartsListBox.length; i++){

myArray.push({"Value": PartsListBox.getSaveItem(i), "DisplayValue":PartsListBox.getDisplayItem(i)});

}

//Sort the value using sortArray Function

myArray.sort(sortArray);

//Insert values in the list box to display sorted values

for (var i = 0; i < myArray.length; i++){

}

//Parameter function to sort values

function sortArray(a,b){

return (isNaN(a.DisplayValue) || isNaN(b.DisplayValue)) ? a.DisplayValue.localeCompare(b.DisplayValue) : verifyZeros(a.DisplayValue,b.DisplayValue) || parseInt(a.DisplayValue) - parseInt(b.DisplayValue);

}

//Function to sort values leading with 0 digits

function verifyZeros(a,b) {

for (var i = 0; i < a.length && i < b.length; i++){

if (parseInt(a.charAt(i)) > parseInt(b.charAt(i))){

return true

}

}

return false;

}  Level 9

This is my unsorted list... and this is after I run your last script...   Level 10

Hi there,

I'm sorry I did not see this, here is a minor change on both function sort and verifyZeros  Level 9

It appears to be sorting the numbers as text instead of as numbers...   Level 10

Well to sort numbers with 0 leading digits there is no other way to sort them the way you want it, because any number with 0 leading digits converted into integers have the zeros removed.. For all numbers with 0 leading digits it sort the text by numbers, but any other number without 0 leading digits it sorts by numbers  Level 9

I really appreacie all the help you have given me. Much appreciated!  Level 9

Maybe I am wrong in how I should be sorting part numbers. What you are saying certainly makes sense. I apologize if I have been asking the wrong questions.  Level 10

What is it you're trying to do? You want the numbers after the letters to be sorted with only all the numbers instead of the whole string?  Level 9

The users can enter any part number that is involved. These part numbers can start with letters or numbers, including some that start with zeros. I want a button the user can click after entering several part numbers that will sort the list so if there are a lot of parts listed, they can find them because they are sorted in a logical order. I didn't realize there were so many ways to sort.  