Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.
SOLVED

Sort listBox that contains text and numbers

Avatar

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

Avatar

Correct answer by
Level 10

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

It references the object to display the results.

View solution in original post

20 Replies

Avatar

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

Avatar

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][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.

Avatar

Level 10

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]);

});

Avatar

Level 9

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

Avatar

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[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]);

});

Avatar

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.

Avatar

Level 10

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

Avatar

Correct answer by
Level 10

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

It references the object to display the results.

Avatar

Level 9

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

Thank you so much!

Avatar

Level 9

Magus069,

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

Avatar

Level 9

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

Avatar

Level 10

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;

}

Avatar

Level 9

I'm still having trouble with part numbers that start with zero.

This is my unsorted list...

UnsortedPartsListBox.PNG

and this is after I run your last script...

SortedPartsListBox.PNG

Avatar

Level 10

Hi there,

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

Avatar

Level 9

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

Capture.PNG

Avatar

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

Avatar

Level 9

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

Avatar

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. 

Avatar

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?

Avatar

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.