3

I have tried:

select sort(obj.displayName, 'lhs < rhs') from my.org.BusinessClass obj

and

select sort(obj.displayName, 'lhs.toString() < rhs.toString()') from my.org.BusinessClass obj

Both give me character array results, but none of them are sorted by String. I think it's sorting by object id.

I have also tried:

select sort(obj.displayName, lhs < rhs) from my.org.BusinessClass obj 
select sort(obj.displayName, lhs.toString() < rhs.toString()) from my.org.BusinessClass obj

But these result in errors because sort second argument is suppose to a string expression.

The examples on the VisualVM documentation are only for numbers: Analyzing a Heap Dump Using Object Query Language (OQL)

The class is structured as follows:

package my.org;
public class BusinessClass {
    private String displayName;
    // rest of class omitted for brevity 
}
joseph
  • 2,429
  • 1
  • 22
  • 43

2 Answers2

4

You can use the following query to sort strings in OQL:

select sort(heap.objects('java.lang.String'), 'lhs.toString().localeCompare(rhs.toString())')

in your case use this OQL query to sort your business object by displayName:

select sort(heap.objects('my.org.BusinessClass'), 'lhs.displayName.toString().localeCompare(rhs.displayName.toString())')

if you want to see your business object and actual displayName in the output use this OQL query:

select map(sort(heap.objects('my.org.BusinessClass'), 'lhs.displayName.toString().localeCompare(rhs.displayName.toString())'), 'toHtml(it)+" "+it.displayName.toString()')
Tomas Hurka
  • 6,723
  • 29
  • 38
  • I tried `select sort(obj.displayName, 'lhs.toString().localeCompare(rhs.toString())') from my.org.BusinessClass obj` but it still seems to sorting by object id instead of the string contents. Does it seen to be heap.objects('java.lang.String') ? – joseph Dec 05 '16 at 15:32
  • 1
    Yes, you have to use heap.objects(). I updated my answer above to exactly match your case. – Tomas Hurka Dec 10 '16 at 10:07
0

This is the workaround I used:

  1. Increase the max number of rows returned by OQL
    1. (see: Increasing the Max Size of OQL Result )
  2. Copy and paste the result of the unsorted query to a text file
  3. Use vim or similar text processor to remove the object id
  4. sort using unix sort
Community
  • 1
  • 1
joseph
  • 2,429
  • 1
  • 22
  • 43