Source language: Translate to:

Sort function

Questions and information about using VBScript and JavaScript in NeoBook functions

Moderator: Neosoft Support

Postby Tony Kroos » Tue Feb 14, 2012 10:26 pm

I'll even give you another short example of the same function:
Code: Select all
nbSetVar ("textlist_sorted", nbGetVar("textlist").split("'").sort().join("'")) ;
nbSetVar ("textlist_reverse_sorted", nbGetVar("textlist").split("'").sort().reverse().join("'")) ;

feel the magic experience? :D
Tony Kroos
 
Posts: 402
Joined: Thu Oct 15, 2009 3:43 pm

Postby dpayer » Wed Feb 15, 2012 7:58 am

Tony Kroos wrote:I'll even give you another short example of the same function:
Code: Select all
nbSetVar ("textlist_sorted", nbGetVar("textlist").split("'").sort().join("'")) ;
nbSetVar ("textlist_reverse_sorted", nbGetVar("textlist").split("'").sort().reverse().join("'")) ;

feel the magic experience? :D


Haha.. yes, I am catching the 'magic' experience of using scripts. I realize I need to perfect my knowledge of functions, methods, and syntax. Having said that, the doors open quite wide with these abilities.

Thanks for your comments and examples.

David P.
User avatar
dpayer
 
Posts: 1380
Joined: Mon Apr 11, 2005 5:55 am
Location: Iowa - USA

Postby dpayer » Wed Feb 15, 2012 11:05 am

This post will look VERY long because I paste in some lists I am working with and the result of processes done to sort them.


Gaev wrote:DavidP:
I am using the tick "`" as a delimiter.

a) Not usually a good idea to use special characters (like the tick mark) as a delimiter.


My lists to sort will contain commas

I have tested that the .sort() works. I am finding errors with the .reverse() process
Gaev wrote:a) What is the error ?
.


I will place my data in code boxes below. Essentially, it appears the .sort() process has limitations. It sorts part and then sorts the rest in a new order (see below). What I was seeing as a problem with the .reverse() process was actually that it showed the original imperfect sort in reverse :)

I think this was likely not intended for anything but small lists ( <50).

Gaev wrote:In order to determine what does not work, you need to take this one step at a time ... when you go from "expected' to "unexpected", you have zeroed in on the problem ....

Some pages on the W3Schools website that will help you ...

http://www.w3schools.com/js/default.asp ... Javascript
http://www.w3schools.com/js/js_obj_array.asp ... Javascript Array Object
http://www.w3schools.com/jsref/default.asp ... Javascript Objects Reference
http://www.w3schools.com/js/js_ex_objects.asp ... Examples of Javascript Array Objects


Thanks for the coaching. I use the w3schools site for the basis of my work. - David


Original list:
Code: Select all
RavenWebLocationAnalyzerConfigurationAndUsageGuide.doc
RavenWebLocationAnalyzerConfigurationAndUsageGuide.pdf
LIM activities-9-30-2011.doc
SCAR-helper.doc
Wave Regression - 866.xlsx
Setting HOME in network analyzyer.doc
Does the call history indicate a legitimate Comm issue.doc
Determining Severity of Comms Issues using Ravenweb Tools.doc
comparison.doc
actionscript-estimated-display.txt
Previous_Computer
neobook-codes.txt
GPS exceptions.xlsx
F2C - docs.doc
working-GPS exceptions.xlsx
working-GPS exceptions.xlsm
temp-GPS-exceptions-2011-20-27.txt
temp-GPS-exceptions-2011-20-27.xlsx
Stats on GPS.doc
GPS data in RavenWeb.doc
GPS exceptions-twodatasource.xlsx
Networkperson.pdf
Additional_GMS_ideas.doc
Book1.xlsx
GPS Data Nov3.xlsx
Stats on GPS-Nov3.doc
GPS-DETAILED-SPECIFIC-report-Nov7.xlsx
GPS reporting via Ravenweb.doc
UPDATE on GPS exceptions-twodatasource (2).xlsx
GPSRPT11-15.xlsx
partialreportonGPSvariations.xls
Test for firewall change.doc
list.txt
printer1.doc
Copy of Wave Regression - Copy.xlsx
Dec20-GPS-data.xlsx
temp-Dec27GPS.xlsx
GPS-NoDevice-VSAT.xls
Dec27GPS.xlsx
CDMA-fix-GPS.xls
tempGPSDec28.xlsx
Jan3-Exceptions.xlsx
100fixGPS.xls
Term Status Export Last 7 days of Raw 2012-01-03 11-36-13 AM.xlsx
Term Status Export Last 7 days of GPS Status 2012-01-05 12-11-22 PM.xlsx
Term Status Export Last 7 days of Raw 2012-01-03 11-36-13 AM.accdb
GPS-reporting
GPSreport1.accdb
MMOMaintainance-error.pdf
Copy of Wave Regression - Copy (3).xlsx
error-regression-testing-pick3-4.doc
FAQs  Raspberry Pi.mht
regressiontesting-template
Copy of GPS_Report_asofNov17.xls
FlashProducts.doc
FlashProducts.pdf
RE request for a quote2.msg
How to close your Google Account - The Washington Post.mht
comparelists.xlsx
Using Conditional Formatting in Excel.doc
listquery
Using Conditional Formatting in Excel.pdf
NeoPlugins
test.wav
NeoBook
UnivercityTutorialReview.pdf
Univercity retailer training tutorial - review.doc
md5.exe
Hasher-Readme.txt
mantis_bug_tracker_administration_guide.pdf
WingDing-sets.rtf
NeoBook 5
oddlist.txt


sorted list - see how it 'starts over' at WingDing-sets.rtf
Code: Select all
100fixGPS.xls,
Additional_GMS_ideas.doc,
BatonInvoice.pdf,
Book1.xlsx,
CDMA-fix-GPS.xls,
Code Green Powerball.msg,
Copy of GPS_Report_asofNov17.xls,
Copy of Wave Regression - Copy (3).xlsx,
Copy of Wave Regression - Copy.xlsx,
Dec20-GPS-data.xlsx,
Dec27GPS.xlsx,
Determining Severity of Comms Issues using Ravenweb Tools.doc,
Does the call history indicate a legitimate Comm issue.doc,
FAQs  Raspberry Pi.mht,
FlashProducts.doc,
FlashProducts.pdf,
GPS Data Nov3.xlsx,
GPS data in RavenWeb.doc,
GPS exceptions-twodatasource.xlsx,
GPS exceptions.xlsx,
GPS reporting via Ravenweb.doc,
GPS-DETAILED-SPECIFIC-report-Nov7.xlsx,
GPS-NoDevice-VSAT.xls,
GPS-reporting,
GPSRPT11-15.xlsx,
GPSreport1.accdb,
Hasher-Readme.txt,
How to close your Google Account - The Washington Post.mht,
IA Extrema Test Script v3.doc,
IowaLIM-Management.xls,
Jan3-Exceptions.xlsx,
LIM Wish List - BJ-Ken.doc,
LIM activities-9-30-2011.doc,
Lottery in Motion - work processes.doc,
Lottery in Motion - work processes.pdf,
MMOMaintainance-error.pdf,
NeoBook,
NeoBook 5,
NeoPlugins,
Networkperson.pdf,
Previous_Computer,
RE request for a quote2.msg,
RavenWebLocationAnalyzerConfigurationAndUsageGuide.doc,
RavenWebLocationAnalyzerConfigurationAndUsageGuide.pdf,
ReligiousFreedomSiteComments.doc,
Request for change in page display of LIM section of GMS.doc,
Request for change in page display of LIM section of GMS.pdf,
Retailers-Region-Ukey.xlsx,
SCAR-helper.doc,
SGI ftp info.rtf,
Setting HOME in network analyzyer.doc,
Stats on GPS-Nov3.doc,
Stats on GPS.doc,
Term Status Export Last 7 days of GPS Status 2012-01-05 12-11-22 PM.xlsx,
Term Status Export Last 7 days of Raw 2012-01-03 11-36-13 AM.accdb,
Term Status Export Last 7 days of Raw 2012-01-03 11-36-13 AM.xlsx,
Test for firewall change.doc,
UPDATE on GPS exceptions-twodatasource (2).xlsx,
Ukey-Regions.xlsx,
Univercity retailer training tutorial - review.doc,
UnivercityTutorialReview.pdf,
Using Conditional Formatting in Excel.doc,
Using Conditional Formatting in Excel.pdf,
Wave Regression - 866.xlsx,
WingDing-sets.rtf,
actionscript-estimated-jackpots-display.txt,
comparelists.xlsx,
comparison.doc,
error-regression-testing-pick3-4.doc,
insights%20into%20the%20afterlife[1].doc,
list.txt,
listquery,
lottery-email-problem.pdf,
mantis_bug_tracker_administration_guide.pdf,
md5.exe,
mileage-lottery-datadrive.xls,
neobook-codes.txt,
oddlist.txt,
partialreportonGPSvariations.xls,
printer1.doc,
regressiontesting-template,
temp-Dec27GPS.xlsx,
temp-GPS-exceptions-2011-20-27.txt,
temp-GPS-exceptions-2011-20-27.xlsx,
tempGPSDec28.xlsx,
test.wav,
working-GPS exceptions.xlsm,
working-GPS exceptions.xlsx,F2C - docs.doc
User avatar
dpayer
 
Posts: 1380
Joined: Mon Apr 11, 2005 5:55 am
Location: Iowa - USA

Postby Gaev » Wed Feb 15, 2012 11:48 am

DavidP:
I will place my data in code boxes below. Essentially, it appears the .sort() process has limitations. It sorts part and then sorts the rest in a new order (see below).

sorted list - see how it 'starts over' at WingDing-sets.rtf

Actually, it is sorted correctly ... the sort is Case Sensitive ... uppercase before lowercase ... and numbers before letters and space before everything else.

If you don't want case sensitivity, try and 'lowercase everything' and then do the sort on the resulting content.
User avatar
Gaev
 
Posts: 3718
Joined: Fri Apr 01, 2005 7:48 am
Location: Toronto, Canada

Postby dpayer » Wed Feb 15, 2012 1:24 pm

Gaev wrote:DavidP:
I will place my data in code boxes below. Essentially, it appears the .sort() process has limitations. It sorts part and then sorts the rest in a new order (see below).

sorted list - see how it 'starts over' at WingDing-sets.rtf

Actually, it is sorted correctly ... the sort is Case Sensitive ... uppercase before lowercase ... and numbers before letters and space before everything else.

If you don't want case sensitivity, try and 'lowercase everything' and then do the sort on the resulting content.


Major duh here. :oops:
This process is beating into me step by step the new environment I am entering. Presumptions are hard to unload!

Thanks.

David P.
User avatar
dpayer
 
Posts: 1380
Joined: Mon Apr 11, 2005 5:55 am
Location: Iowa - USA

Postby dpayer » Wed Feb 15, 2012 4:06 pm

Many thanks to Gaev and Tony Kroos to help me develop stronger scripting skills.

I have solved the sorting problem to my satisfaction. To address the upper/lower case issue, I copy the line, make it lower case, append it to the original line with upper/lower with a delineator between them. Then I sort. When done, I strip out the lowercaseonly portion and write a new array with the original value.

I then push the value back to NB.

Code: Select all
var textlist=nbGetVar( "textlist" );
var textlistarray=textlist.split("`");
var textarraylen=textlistarray.length;

var newtextarray=new Array();
var new2textarray=new Array();

var i=0;
for (i=0;i<textarraylen;i++)
{var temp1=textlistarray[i].toLowerCase();
var temp2=textlistarray[i];
var temp3="~~~";
var temp4=temp1+temp3+temp2;
newtextarray[i]=temp4;
}
newtextarray.sort();

var z=0;
for (z=0;z<textarraylen;z++)
{
var temp2val=newtextarray[z];
var tempval=temp2val.split(temp3);
new2textarray[z]=tempval[1];
delete tempval;
}

nbSetVar ( "text2list", new2textarray.join() ) ;
User avatar
dpayer
 
Posts: 1380
Joined: Mon Apr 11, 2005 5:55 am
Location: Iowa - USA

Postby Tony Kroos » Wed Feb 15, 2012 8:45 pm

your code still can be greatly reduced and optimized, think about it
Tony Kroos
 
Posts: 402
Joined: Thu Oct 15, 2009 3:43 pm

Postby dpayer » Wed Feb 15, 2012 9:19 pm

Tony Kroos wrote:your code still can be greatly reduced and optimized, think about it


I will Tony.

First: I want to make it work
Second: I want to understand the processes

Then I'll look for efficiencies. I know I can consolodate some of the variable statements but I am still catching the syntax. Small things make a difference!

Again, thanks for your help.

David
User avatar
dpayer
 
Posts: 1380
Joined: Mon Apr 11, 2005 5:55 am
Location: Iowa - USA

Postby Tony Kroos » Wed Feb 15, 2012 11:58 pm

so, try this and tell me if it works for you (a case insensitive sort)
http://www.mediafire.com/file/q4smhtcjd ... jssort.zip

(I had to use a regular expressions tricks (.replace) to delete a leading [#10] symbol from result string. Source string is taken from Listbox with pipe delimiters and "joined" again with [#10] delimiters to fit into Listbox again)
Tony Kroos
 
Posts: 402
Joined: Thu Oct 15, 2009 3:43 pm

Postby Gaev » Thu Feb 16, 2012 6:56 am

DavidP:

I Googled "javascript sort case insensitive" and found some examples ... here is one that I tested in NeoBook ...
Code: Select all
//gkTest4DavidP2
var passedText = nbGetVar( "nbTextList" );
nbSetVar('answer1',passedText);
originalList = passedText.split(",");
sortedList= originalList.sort(caseInsensitiveSort);
nbSetVar('answer2',sortedList);
sortedString = sortedList.toString();
nbSetVar('answer3',sortedString);

function caseInsensitiveSort(a, b)
{
   var ret = 0;
   a = a.toLowerCase();b = b.toLowerCase();
   if(a > b)
      ret = 1;
   if(a < b)
      ret = -1;
   return ret;
}
Note that ...

1) I really don't understand "why the specification of a function in the .sort() method does what it does" ... or even "what passed parameters a and b are supposed to represent".

I am guessing that the Javascript .sort method is doing some sort of "bubble sort" and calls the specified function each time it is comparing an Array Item (a) with an adjacent one (b) ... and the function is returning a value (-1 or 0 or 1) to indicate the relative sort order of the two items.

But it works !!!

2) It sorts Banana before banana ... and banana before Binana.

So, after all the exchanges, it comes down to ...

originalList = passedText.split(",");
sortedList= originalList.sort(caseInsensitiveSort);

... and a function{} that is a mystery but works
User avatar
Gaev
 
Posts: 3718
Joined: Fri Apr 01, 2005 7:48 am
Location: Toronto, Canada

Postby Tony Kroos » Thu Feb 16, 2012 7:29 am

Gaev wrote:I am guessing that the Javascript .sort method is doing some sort of "bubble sort" and calls the specified function each time it is comparing an Array Item (a) with an adjacent one (b) ... and the function is returning a value (-1 or 0 or 1) to indicate the relative sort order of the two items.

A wiki about algorithm used in js sort() method and explanation.
a and b are the parameters (two array elements which are compared with each other) passed by the sort method to function.
Tony Kroos
 
Posts: 402
Joined: Thu Oct 15, 2009 3:43 pm

Previous

Return to NeoBook Functions - VBScript & JavaScript

Who is online

Users browsing this forum: No registered users and 0 guests

cron