Home > Back-end >  Java language to write an algorithm to deal with a list of data, to a great god to solve it
Java language to write an algorithm to deal with a list of data, to a great god to solve it

Time:05-11

The original list data:
"A list" : [
{" id ": 54," pid ": 0," name ":" taxes payable ", "level" : 1, "code" : "2221"},
{" id ": 55," pid ": 54," name ":" value added tax payable ", "level" : 2, "code" : "222101"},
{" id ": 56," pid ": 55," name ":" the input tax ", "level" : 3, "code" : "22210101"},
57, {" id ":" pid ": 55," name ":" the output tax deduction "and" level ": 3," code ":" 22210102 "},
{" id ": 58," pid ": 54," name ":" not pay value-added tax ", "level" : 2, "code" : "222102"},
{" id ": 59," pid ": 54," name ":" accepting the VAT ", "level" : 2, "code" : "222103"},
.
{" id ": 158," pid ": 0," name ":" interest ", "level" : 1, "code" : "2223"},
{" id ": 159," pid ": 54," name ":" to offset the input tax ", "level" : 2, "code" : "222104"},
{" id ": 160," pid ": 55," name ":" has to pay taxes ", "level" : 3, "code" : "22210103"},
]

To achieve the demand of:
"A list" : [
{" id ": 54," pid ": 0," name ":" taxes payable ", "level" : 1, "code" : "2221"},
{" id ": 55," pid ": 54," name ":" value added tax payable ", "level" : 2, "code" : "222101"},
{" id ": 56," pid ": 55," name ":" the input tax ", "level" : 3, "code" : "22210101"},
57, {" id ":" pid ": 55," name ":" the output tax deduction "and" level ": 3," code ":" 22210102 "},
{" id ": 160," pid ": 55," name ":" has to pay taxes ", "level" : 3, "code" : "22210103"},
{" id ": 58," pid ": 54," name ":" not pay value-added tax ", "level" : 2, "code" : "222102"},
{" id ": 59," pid ": 54," name ":" accepting the VAT ", "level" : 2, "code" : "222103"},
{" id ": 159," pid ": 54," name ":" to offset the input tax ", "level" : 2, "code" : "222104"},
.
{" id ": 158," pid ": 0," name ":" interest ", "level" : 1, "code" : "2223"},
]


Is similar to a tree data structure, the children always under the parent node, and according to the size of the code for the sorting,



Here is a picture look at the wanted list format



CodePudding user response:

This is a tree structure types, according to the tree structure processing is ok; Can be dealt with according to the recursive

CodePudding user response:

List List=//your collection;
The Collections. The sort (list, new Comparator () {
@ Override
Public int the compare (Tom o1, o2) Tom {
Return o1. GetName (). CompareTo (o2) getName ());
}
});
Tom into your List of objects in the collection,

CodePudding user response:

Forgot to say, o1. GetName () compareTo (o2) getName ()); Change your o1. GetCode (.) compareTo (o2) getCode ());
If field code is not a String, converted to a String, the String. The valueOf (o1, getCode ()). The compareTo (String) the valueOf (o2, getCode ()));

CodePudding user response:

Collections. Sort, custom collation, you look at your request in accordance with the positive sequence coding sequence is ok

CodePudding user response:

Collections. Sort, step cannot solve the problem

CodePudding user response:

With the database
  • Related