Sort listBox that contains text and numbers

DKinsley

07-10-2016

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

Accepted Solutions (1)

Accepted Solutions (1)

Answers (19)

Answers (19)

DKinsley

17-10-2016

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.

Magus069

MVP

14-10-2016

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?

DKinsley

14-10-2016

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. 

Magus069

MVP

14-10-2016

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

Magus069

MVP

11-10-2016

Here try this instead!

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++){

     SortedPartsListBox.addItem(myArray[i].DisplayValue, myArray[i].Value);

}

//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;

}

DKinsley

11-10-2016

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.

radzmar

MVP

11-10-2016

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[0])),

  testB = isRealNaN(parseFloat(b[0]));

  // A is NaN

  if (testA === true) {

  // B is NaN

  if (testB === true) {

  if (a[0] < b[0]) {

  return -1;

  }

  if (a[0] > b[0]) {

  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[0] - b[0];

  }

  }

  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[0], element[1]);

});

DKinsley

11-10-2016

No, unfortunitly that code does not work. If I enter the following part numbers:

Pic1.png

and then click the Sort Parts List button containing the code, this is what I get:

Pic2.png

radzmar

MVP

10-10-2016

Try this code:

var oSource = PartsListBox,

  aList = [],

  sortValues = function(a, b) {

  return a[0] - b[0];

  };

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[0], element[1]);

});

DKinsley

10-10-2016

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][0] = PartsListBox.getSaveItem(i);
temp[i][1] = 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++)
{
PartsListBox.addItem(temp[i][1], temp[i][0]);
}

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

Magus069

MVP

07-10-2016

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