Hi everyone! I need to sort a saved search by 3 pa...
# suitescript
v
Hi everyone! I need to sort a saved search by 3 parameters in a particular order. In UI you simply specify those in Saved Search on Results subtab(sort by param1, then by param2, then by param3). Using N/search module in SuiteScript 2.0 I haven't found any way to specify the sorting order itself. You just add sort option in your search.createColumn({...}) method. I'm not sure how NetSuite understands in which order my saved search is to be sorted(param1, param2,param3 or param2, param1, param3 etc.) Any ideas? Thanks!
j
You could concat the three parameters with padding between each param at a set length in a single column with a formula... like "Param1*********Param2********Param3" and sort by that single column.
s
I have used the concatenated-formula-sort approach before and it works fairly well. Another option is to do the sorting in code. Then you have a lot of flexibility with how you want to sort the results.
n
have you tried creating the Search in the UI and exporting it using the chrome extension to see what it looks like?
v
Sorting in code is what i'm looking for. I use NetSuite Scripted Records Chrome extension for exporting saved search in SuiteScript. After I specify sorting parameters in UI I export saved search with that extension and can use it in IDE. The problem is that when I sort it in param1, param2, param3 order, exported saved search have no idea in what order to sort.
😞 1
k
Doesn't sort order in 2.0 just depend on which order you define your columns?
so the first column with sort will be sorted first, the second column with sort will be second, and so on?
It is an indexed array after all.
s
Sorry, I should have been more specific. I meant that one option is to place your search results into a javascript array of objects (each result iteration, push a new object containing all of the columns into the array). After you have pushed all of the results into your custom array, you can use an array sort function (you can design the sort function however you like) :
Copy code
resultArray.sort(function(a, b) {
  if (a.param1 === b.param1) {
    if (a.param2 === b.param2) {
	  if (a.param3 < b.param3) { return -1; } else { return 1; }
	}
	if (a.param2 < b.param2) { return -1; } else { return 1; }
  }
  if (a.param1 < b.param1) { return -1; } else { return 1; }
})
v
@Kris Wood it would be a straightforward approach, however, after i export sorted saved search(sorted first by a, then by b, then by c), i can see that sorting order in code is kinda messed up(first - column b, then a, then c). It looks like it sorts correctly(at least yet), though. But I would like to be 100% sure it does the right job. Thanks!