Recent

Author Topic: json sort  (Read 9336 times)

anna

  • Sr. Member
  • ****
  • Posts: 426
json sort
« on: July 12, 2013, 06:26:23 am »

 Suppose , I have json-file. It contain array of many subarrays, each subarray contain a 2 values name and id. How to sort subarrays by id (whithout of viewing all tree and bubble sort. I see JSON data viewer demo. I think that parser already viewed all nodes, so sorting must be much quicker. But I cannot find any method for sorting. SORT-method sorts only top level-nodes  )?
WinXP SP3 Pro Russian 32-bit (5.1.2600)

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4660
  • I like bugs.
Re: json sort
« Reply #1 on: July 12, 2013, 11:19:30 am »
Suppose , I have json-file. It contain array of many subarrays, each subarray contain a 2 values name and id. How to sort subarrays by id (whithout of viewing all tree and bubble sort. I see JSON data viewer demo. I think that parser already viewed all nodes, so sorting must be much quicker. But I cannot find any method for sorting. SORT-method sorts only top level-nodes  )?

You are testing the tools/jsonviewer demo, right?
TMainForm.ShowJSONData sorts the data recursively (not only top level-nodes) when FSortObjectMembers flag is set, but it sorts by name string only.
For your own sort order you can either copy and modify ShowJSONData and make it sort by ID, or you can use TTreeView.CustomSort() providing a compare function for it.
I don't know why the demo itself does not use CustomSort().
Bubble sort is not needed in any situation.
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux 64-bit.

anna

  • Sr. Member
  • ****
  • Posts: 426
Re: json sort
« Reply #2 on: July 12, 2013, 05:44:48 pm »
...you can either copy and modify ShowJSONData and make it sort by ID...
ShowJSONData does not sort by name. It uses TStringList.Sort method. For example I have a json structure:

arr_name
--subarr_name1
----firstname : Gabriel
----id: 4
--subarr_name2
----firstname : Anna
----id: 2
--subarr_name3
----firstname : Elizabeth
----id: 1
--subarr_name4
----firstname : Bohdan
----id: 3

Sorted json-file must be like this:

arr_name
--subarr_name3
----firstname : Elizabeth
----id: 1
--subarr_name2
----firstname : Anna
----id: 2
--subarr_name4
----firstname : Bohdan
----id: 3
--subarr_name1
----firstname : Gabriel
----id: 4

CustomSort provides a chance to define a compare function. But what parameters I must  set?
« Last Edit: July 12, 2013, 06:20:02 pm by anna »
WinXP SP3 Pro Russian 32-bit (5.1.2600)

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: json sort
« Reply #3 on: July 12, 2013, 06:19:24 pm »
The same parameters as this function:

Code: [Select]
function StringListAnsiCompare(List: TStringList; Index1, Index: Integer): Integer;   

anna

  • Sr. Member
  • ****
  • Posts: 426
Re: json sort
« Reply #4 on: July 12, 2013, 07:22:52 pm »
The same parameters as this function:

Code: [Select]
function StringListAnsiCompare(List: TStringList; Index1, Index: Integer): Integer;   

Does CustomSort have any interaction of parent nodes? It sorts list, not a tree.
WinXP SP3 Pro Russian 32-bit (5.1.2600)

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4660
  • I like bugs.
Re: json sort
« Reply #5 on: July 12, 2013, 09:44:05 pm »
The same parameters as this function:
Code: [Select]
function StringListAnsiCompare(List: TStringList; Index1, Index: Integer): Integer;   

Typo, why you give such false information to anna?
The parameter for TTreeView.CustomSort is : SortProc: TTreeNodeCompare
Code: [Select]
  TTreeNodeCompare = function(Node1, Node2: TTreeNode): integer of object;

It can easily be seen by browsing the code with Ctrl-Click.
Anna, did you not know about the Ctrl-Click on an identifier feature?
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux 64-bit.

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: json sort
« Reply #6 on: July 12, 2013, 09:55:10 pm »
Sorry, I misunderstood the question.

anna

  • Sr. Member
  • ****
  • Posts: 426
Re: json sort
« Reply #7 on: July 13, 2013, 12:45:49 pm »
For your own sort order you can either copy and modify ShowJSONData and make it sort by ID

I don't understand. Sort method sorts only a same-level names of json-objects and it cannot to get into other same-level but of different parent. Sort method even can't access to values, just to names. Sort method does not rearrange parent names according to some child object.

How to modify ShowJSONData? It's impossible, I think.
« Last Edit: July 13, 2013, 12:54:34 pm by anna »
WinXP SP3 Pro Russian 32-bit (5.1.2600)

ludob

  • Hero Member
  • *****
  • Posts: 1173
Re: json sort
« Reply #8 on: July 13, 2013, 02:11:25 pm »
Your JSON structure is containing an array of objects instead of an array of subarrays.  TJSONArray has an Exchange method that allows swapping of array elements. The data is stored internally in a TFPObjectList so accessing and swapping elements is fast. So, to sort an array of objects you can do a QuickSort on the TJSONArray where, in your case, you compare TJSONObject(JSONArray[ i ]).Integers[' id' ] with TJSONObject(JSONArray[ j ]).Integers[ 'id' ]. See rtl/objpas/classes/lists.inc for a QuickSort implementation that can be easily adapted for this purpose.

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4660
  • I like bugs.
Re: json sort
« Reply #9 on: July 13, 2013, 02:18:14 pm »
I don't understand. Sort method sorts only a same-level names of json-objects and it cannot to get into other same-level but of different parent. Sort method even can't access to values, just to names. Sort method does not rearrange parent names according to some child object.

If you look carefully, ShowJSONData calls itself giving a child node as a new AParent parameter.
The StringList is used to sort just one level, the siblings. That is how a tree must be sorted, otherwise the tree structure would change.
A tree data structure is inherently recursive, that is why a recursive function call is the most natural way to manipulate it.

In this case the data comes from a JSON file but it is not important. We could be now sorting any tree data.
See how the StringList is populated. There is Data and its sub-items Data.items. You have access to all data items and you can do whatever you want with them.

Quote
How to modify ShowJSONData? It's impossible, I think.

Everything is possible. :)
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux 64-bit.

ludob

  • Hero Member
  • *****
  • Posts: 1173
Re: json sort
« Reply #10 on: July 13, 2013, 03:13:53 pm »
In this case the data comes from a JSON file but it is not important. We could be now sorting any tree data.
The title says json sort. Here you talk about sorting in a treeview which, as you noted, has nothing to do with JSON. Could OP please be more specific? Is writing back of the sorted JSON a requirement or is this just all about displaying?

 

TinyPortal © 2005-2018