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
Solved! Go to Solution.
Views
Replies
Total Likes
You may have to change this line:
oTarget = this,
It references the object to display the results.
Views
Replies
Total Likes
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
Views
Replies
Total Likes
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.
Views
Replies
Total Likes
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]);
});
Views
Replies
Total Likes
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:
Views
Replies
Total Likes
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]);
});
Views
Replies
Total Likes
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.
Views
Replies
Total Likes
If I understand well, this might do the trick then... not sure if it fits your needs but I surely hope so.
Views
Replies
Total Likes
You may have to change this line:
oTarget = this,
It references the object to display the results.
Views
Replies
Total Likes
Yes! That's what it needed. I changed "this" to PartsListBox and it works!
Thank you so much!
Views
Replies
Total Likes
Magus069,
Thank you for your help trying to solve this sort issue. It has been solved.
Views
Replies
Total Likes
The only problem I stilll have is part numbers that start with 0 like 015645 do not sort correctly.
Views
Replies
Total Likes
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;
}
Views
Replies
Total Likes
I'm still having trouble with part numbers that start with zero.
This is my unsorted list...
and this is after I run your last script...
Views
Replies
Total Likes
Hi there,
I'm sorry I did not see this, here is a minor change on both function sort and verifyZeros
Views
Replies
Total Likes
It appears to be sorting the numbers as text instead of as numbers...
Views
Replies
Total Likes
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
Views
Replies
Total Likes
I really appreacie all the help you have given me. Much appreciated!
Views
Replies
Total Likes
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.
Views
Replies
Total Likes
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?
Views
Replies
Total Likes
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.
Views
Replies
Total Likes
Views
Likes
Replies
Views
Likes
Replies
Views
Likes
Replies