Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

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

Learn More

View all

Sign in to view all badges

Sort listBox that contains text and numbers

Avatar

Avatar
Validate 100
Level 3
DKinsley
Level 3

Likes

9 likes

Total Posts

545 posts

Correct Reply

5 solutions
Top badges earned
Validate 100
Validate 50
Validate 25
Validate 10
Validate 1
View profile

Avatar
Validate 100
Level 3
DKinsley
Level 3

Likes

9 likes

Total Posts

545 posts

Correct Reply

5 solutions
Top badges earned
Validate 100
Validate 50
Validate 25
Validate 10
Validate 1
View profile
DKinsley
Level 3

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)

Avatar

Avatar
Coach
MVP
radzmar
MVP

Likes

395 likes

Total Posts

2,686 posts

Correct Reply

491 solutions
Top badges earned
Coach
Ignite 3
Ignite 1
Validate 1
Give Back 50
View profile

Avatar
Coach
MVP
radzmar
MVP

Likes

395 likes

Total Posts

2,686 posts

Correct Reply

491 solutions
Top badges earned
Coach
Ignite 3
Ignite 1
Validate 1
Give Back 50
View profile
radzmar
MVP

11-10-2016

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

It references the object to display the results.

Answers (19)

Answers (19)

Avatar

Avatar
Validate 100
Level 3
DKinsley
Level 3

Likes

9 likes

Total Posts

545 posts

Correct Reply

5 solutions
Top badges earned
Validate 100
Validate 50
Validate 25
Validate 10
Validate 1
View profile

Avatar
Validate 100
Level 3
DKinsley
Level 3

Likes

9 likes

Total Posts

545 posts

Correct Reply

5 solutions
Top badges earned
Validate 100
Validate 50
Validate 25
Validate 10
Validate 1
View profile
DKinsley
Level 3

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.

Avatar

Avatar
Springboard
MVP
Magus069
MVP

Likes

143 likes

Total Posts

1,077 posts

Correct Reply

155 solutions
Top badges earned
Springboard
Validate 1
Ignite 3
Ignite 1
Establish
View profile

Avatar
Springboard
MVP
Magus069
MVP

Likes

143 likes

Total Posts

1,077 posts

Correct Reply

155 solutions
Top badges earned
Springboard
Validate 1
Ignite 3
Ignite 1
Establish
View profile
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?

Avatar

Avatar
Validate 100
Level 3
DKinsley
Level 3

Likes

9 likes

Total Posts

545 posts

Correct Reply

5 solutions
Top badges earned
Validate 100
Validate 50
Validate 25
Validate 10
Validate 1
View profile

Avatar
Validate 100
Level 3
DKinsley
Level 3

Likes

9 likes

Total Posts

545 posts

Correct Reply

5 solutions
Top badges earned
Validate 100
Validate 50
Validate 25
Validate 10
Validate 1
View profile
DKinsley
Level 3

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. 

Avatar

Avatar
Validate 100
Level 3
DKinsley
Level 3

Likes

9 likes

Total Posts

545 posts

Correct Reply

5 solutions
Top badges earned
Validate 100
Validate 50
Validate 25
Validate 10
Validate 1
View profile

Avatar
Validate 100
Level 3
DKinsley
Level 3

Likes

9 likes

Total Posts

545 posts

Correct Reply

5 solutions
Top badges earned
Validate 100
Validate 50
Validate 25
Validate 10
Validate 1
View profile
DKinsley
Level 3

14-10-2016

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

Avatar

Avatar
Springboard
MVP
Magus069
MVP

Likes

143 likes

Total Posts

1,077 posts

Correct Reply

155 solutions
Top badges earned
Springboard
Validate 1
Ignite 3
Ignite 1
Establish
View profile

Avatar
Springboard
MVP
Magus069
MVP

Likes

143 likes

Total Posts

1,077 posts

Correct Reply

155 solutions
Top badges earned
Springboard
Validate 1
Ignite 3
Ignite 1
Establish
View profile
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

Avatar

Avatar
Validate 100
Level 3
DKinsley
Level 3

Likes

9 likes

Total Posts

545 posts

Correct Reply

5 solutions
Top badges earned
Validate 100
Validate 50
Validate 25
Validate 10
Validate 1
View profile

Avatar
Validate 100
Level 3
DKinsley
Level 3

Likes

9 likes

Total Posts

545 posts

Correct Reply

5 solutions
Top badges earned
Validate 100
Validate 50
Validate 25
Validate 10
Validate 1
View profile
DKinsley
Level 3

13-10-2016

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

Capture.PNG

Avatar

Avatar
Springboard
MVP
Magus069
MVP

Likes

143 likes

Total Posts

1,077 posts

Correct Reply

155 solutions
Top badges earned
Springboard
Validate 1
Ignite 3
Ignite 1
Establish
View profile

Avatar
Springboard
MVP
Magus069
MVP

Likes

143 likes

Total Posts

1,077 posts

Correct Reply

155 solutions
Top badges earned
Springboard
Validate 1
Ignite 3
Ignite 1
Establish
View profile
Magus069
MVP

13-10-2016

Hi there,

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

Avatar

Avatar
Validate 100
Level 3
DKinsley
Level 3

Likes

9 likes

Total Posts

545 posts

Correct Reply

5 solutions
Top badges earned
Validate 100
Validate 50
Validate 25
Validate 10
Validate 1
View profile

Avatar
Validate 100
Level 3
DKinsley
Level 3

Likes

9 likes

Total Posts

545 posts

Correct Reply

5 solutions
Top badges earned
Validate 100
Validate 50
Validate 25
Validate 10
Validate 1
View profile
DKinsley
Level 3

13-10-2016

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

Avatar
Springboard
MVP
Magus069
MVP

Likes

143 likes

Total Posts

1,077 posts

Correct Reply

155 solutions
Top badges earned
Springboard
Validate 1
Ignite 3
Ignite 1
Establish
View profile

Avatar
Springboard
MVP
Magus069
MVP

Likes

143 likes

Total Posts

1,077 posts

Correct Reply

155 solutions
Top badges earned
Springboard
Validate 1
Ignite 3
Ignite 1
Establish
View profile
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;

}

Avatar

Avatar
Validate 100
Level 3
DKinsley
Level 3

Likes

9 likes

Total Posts

545 posts

Correct Reply

5 solutions
Top badges earned
Validate 100
Validate 50
Validate 25
Validate 10
Validate 1
View profile

Avatar
Validate 100
Level 3
DKinsley
Level 3

Likes

9 likes

Total Posts

545 posts

Correct Reply

5 solutions
Top badges earned
Validate 100
Validate 50
Validate 25
Validate 10
Validate 1
View profile
DKinsley
Level 3

11-10-2016

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

Avatar

Avatar
Validate 100
Level 3
DKinsley
Level 3

Likes

9 likes

Total Posts

545 posts

Correct Reply

5 solutions
Top badges earned
Validate 100
Validate 50
Validate 25
Validate 10
Validate 1
View profile

Avatar
Validate 100
Level 3
DKinsley
Level 3

Likes

9 likes

Total Posts

545 posts

Correct Reply

5 solutions
Top badges earned
Validate 100
Validate 50
Validate 25
Validate 10
Validate 1
View profile
DKinsley
Level 3

11-10-2016

Magus069,

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

Avatar

Avatar
Validate 100
Level 3
DKinsley
Level 3

Likes

9 likes

Total Posts

545 posts

Correct Reply

5 solutions
Top badges earned
Validate 100
Validate 50
Validate 25
Validate 10
Validate 1
View profile

Avatar
Validate 100
Level 3
DKinsley
Level 3

Likes

9 likes

Total Posts

545 posts

Correct Reply

5 solutions
Top badges earned
Validate 100
Validate 50
Validate 25
Validate 10
Validate 1
View profile
DKinsley
Level 3

11-10-2016

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

Thank you so much!

Avatar

Avatar
Springboard
MVP
Magus069
MVP

Likes

143 likes

Total Posts

1,077 posts

Correct Reply

155 solutions
Top badges earned
Springboard
Validate 1
Ignite 3
Ignite 1
Establish
View profile

Avatar
Springboard
MVP
Magus069
MVP

Likes

143 likes

Total Posts

1,077 posts

Correct Reply

155 solutions
Top badges earned
Springboard
Validate 1
Ignite 3
Ignite 1
Establish
View profile
Magus069
MVP

11-10-2016

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

Avatar

Avatar
Validate 100
Level 3
DKinsley
Level 3

Likes

9 likes

Total Posts

545 posts

Correct Reply

5 solutions
Top badges earned
Validate 100
Validate 50
Validate 25
Validate 10
Validate 1
View profile

Avatar
Validate 100
Level 3
DKinsley
Level 3

Likes

9 likes

Total Posts

545 posts

Correct Reply

5 solutions
Top badges earned
Validate 100
Validate 50
Validate 25
Validate 10
Validate 1
View profile
DKinsley
Level 3

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.

Avatar

Avatar
Coach
MVP
radzmar
MVP

Likes

395 likes

Total Posts

2,686 posts

Correct Reply

491 solutions
Top badges earned
Coach
Ignite 3
Ignite 1
Validate 1
Give Back 50
View profile

Avatar
Coach
MVP
radzmar
MVP

Likes

395 likes

Total Posts

2,686 posts

Correct Reply

491 solutions
Top badges earned
Coach
Ignite 3
Ignite 1
Validate 1
Give Back 50
View profile
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]);

});

Avatar

Avatar
Validate 100
Level 3
DKinsley
Level 3

Likes

9 likes

Total Posts

545 posts

Correct Reply

5 solutions
Top badges earned
Validate 100
Validate 50
Validate 25
Validate 10
Validate 1
View profile

Avatar
Validate 100
Level 3
DKinsley
Level 3

Likes

9 likes

Total Posts

545 posts

Correct Reply

5 solutions
Top badges earned
Validate 100
Validate 50
Validate 25
Validate 10
Validate 1
View profile
DKinsley
Level 3

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

Avatar

Avatar
Coach
MVP
radzmar
MVP

Likes

395 likes

Total Posts

2,686 posts

Correct Reply

491 solutions
Top badges earned
Coach
Ignite 3
Ignite 1
Validate 1
Give Back 50
View profile

Avatar
Coach
MVP
radzmar
MVP

Likes

395 likes

Total Posts

2,686 posts

Correct Reply

491 solutions
Top badges earned
Coach
Ignite 3
Ignite 1
Validate 1
Give Back 50
View profile
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]);

});

Avatar

Avatar
Validate 100
Level 3
DKinsley
Level 3

Likes

9 likes

Total Posts

545 posts

Correct Reply

5 solutions
Top badges earned
Validate 100
Validate 50
Validate 25
Validate 10
Validate 1
View profile

Avatar
Validate 100
Level 3
DKinsley
Level 3

Likes

9 likes

Total Posts

545 posts

Correct Reply

5 solutions
Top badges earned
Validate 100
Validate 50
Validate 25
Validate 10
Validate 1
View profile
DKinsley
Level 3

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.

Avatar

Avatar
Springboard
MVP
Magus069
MVP

Likes

143 likes

Total Posts

1,077 posts

Correct Reply

155 solutions
Top badges earned
Springboard
Validate 1
Ignite 3
Ignite 1
Establish
View profile

Avatar
Springboard
MVP
Magus069
MVP

Likes

143 likes

Total Posts

1,077 posts

Correct Reply

155 solutions
Top badges earned
Springboard
Validate 1
Ignite 3
Ignite 1
Establish
View profile
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