A few days ago, I received a requirement that the third-party data be displayed in the form of tree, but there is no hierarchy distinction and only the corresponding parent ID. But the data provided by party A is really deceiving, there is no hierarchy, even the data are chaotic. There were three ways to do it, but they wanted to trust that their data wouldn’t change, and then they changed it. Therefore, the only way to set the hierarchy field is to query whether the parent node exists.
Because the data is messy, the subset is on top of the parent, so the parent is not inserted, and the system treats the subset as the parent.
So we came up with bubble sort. Because my data format is JSON, I can’t sort it directly. I think of two ways to do this:
2. Convert json to an objectCopy the code
I’m using the object approach. Without further ado, get right to the code.
List<FolderDIO> List = jsonObject.parsearray (json.tojsonString (), folderdio.class); for (int i = 0; i < list.size() - 1; i++) { for (int j = 1; j < list.size() - i; j++) { FolderDIO folderDIO; // Compare the unique identifier, If ((list.get(j-1).getinDocno ()).compareto (list.get(j).getinDocno ()) > 0) {folderDIO = list.get(j-1); list.set((j - 1), list.get(j)); list.set(j, folderDIO); }}}Copy the code
Bubble sort I think you all know I’m not going to explain it.
In a nutshell, it compares unique identifiers.
The sorted data structure is as follows:
This ensures that the parent inserts before the child.
Get the code where the parent exists:
If there is a wrong place or better way to ask you to advise.