Ask a Question

Sorting an Array Object from FindAll

SOLVED
SuperTester
Contributor

Sorting an Array Object from FindAll

Hey guys!

 

I'm trying to figure out how to sort an array of objects by a certain property. However, I'm encountering an error where TC is saying "tableArray.sort is not a function".

Code:

const tableArray = window.FindAll("JavaClassName","UITablePeer$23",2000,true)

var sortedTableArray = tableArray.sort(function(a,b){return a.Left - b.Left})

 

Now, I know that TC supports the .sort() method because this example line of code does work:

const cars = [
{type:"Volvo", year:2016},
{type:"Saab", year:2001},
{type:"BMW", year:2010}
];

var sortedCarArray = cars.sort(function(a, b){return a.year - b.year});

 

My hunch is that there is a type difference between the tableArray and the cars array. In debugger, the cars array is a Array type whereas tableArray is a ComSafeArray. Could that be the reason for the discrepancy?

 

I tried declaring the resulting object from FindAll as a new array and it was just indexed into an array!

var tableArray = new Array(window.FindAllChildren("JavaClassName","UITablePeer$23",2000,true))

 

SuperTester_0-1632436297482.png

 

Thanks in advance!

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
SuperTester
Contributor

Re: Sorting an Array Object from FindAll

Hey @Marsha_R

I think I just figured it out.

 

By adding the .toArray command, the comSafeArray was converted to an array object and I can use the sort method.

 

var tableArray = window.FindAllChildren("JavaClassName","UITablePeer$23",2000,true).toArray()

var sortedTableArray = tableArray.sort(function(a,b){return a.Left - b.Left})

 

Now, the documentation for the FindAll and FindAll children does warn that  JScript, C#Script and C++Script users need to convert using toArray(), but I did not think this applied to me since I'm coding in Javascript.

 

 

View solution in original post

2 REPLIES 2
Marsha_R
Community Hero

Re: Sorting an Array Object from FindAll

That's a good question to ask Support directly.  Here's the link:

https://support.smartbear.com/testcomplete/message

Marsha_R
[Community Hero]
____
[Community Heroes] are not employed by SmartBear Software but
are just volunteers who have some experience with the tools by SmartBear Software
and a desire to help others. Posts made by [Community Heroes]
may differ from the official policies of SmartBear Software and should be treated
as the own private opinion of their authors and under no circumstances as an
official answer from SmartBear Software.
The [Community Hero] signature is used with permission by SmartBear Software.
https://community.smartbear.com/t5/custom/page/page-id/hall-of-fame
SuperTester
Contributor

Re: Sorting an Array Object from FindAll

Hey @Marsha_R

I think I just figured it out.

 

By adding the .toArray command, the comSafeArray was converted to an array object and I can use the sort method.

 

var tableArray = window.FindAllChildren("JavaClassName","UITablePeer$23",2000,true).toArray()

var sortedTableArray = tableArray.sort(function(a,b){return a.Left - b.Left})

 

Now, the documentation for the FindAll and FindAll children does warn that  JScript, C#Script and C++Script users need to convert using toArray(), but I did not think this applied to me since I'm coding in Javascript.

 

 

View solution in original post

cancel
Showing results for 
Search instead for 
Did you mean: