## java实现对树形结构(文件夹式)数据数组进行排序

wf2311 发布于 2015/05/10 14:33

```Folder有如下属性：id,pid(父节点)，sequence(同级节点排序依据)

F1 (1,0,1)
F2 (2,0,2)
F3 (3,4,1)
F4 (4,2,1)
F5 (5,1,1)
F6 (6,1,2)
F7 (7,6,1)
F8 (8,0,3)

(1,0,1) F1
(5,1,1)      F5

(6,1,2)      F6

(7,6,1)           F7

(2,0,2) F2
(4,2,1)        F4

(3,4,1)           F3
(8,0,3) F8

F1、F5、F6、F7、F2、F4、F3、F8```

0
```import java.util.ArrayList;
import java.util.List;

public class FolderTree
{

public static void main(String[] args)
{
int[] F1 = { 1, 0, 1 };
int[] F2 = { 2, 0, 2 };
int[] F3 = { 3, 4, 1 };
int[] F4 = { 4, 2, 1 };
int[] F5 = { 5, 1, 1 };
int[] F6 = { 6, 1, 2 };
int[] F7 = { 7, 6, 1 };
int[] F8 = { 8, 0, 3 };

List<int[]> list = new ArrayList<int[]>();

LinkedList<int[]> f = sort(list, result, 0);
for(int i = 0; i < f.size(); i++)
{
System.out.print(f.get(i)[0] + ",");
System.out.print(f.get(i)[1] + ",");
System.out.println(f.get(i)[2]);
}
}

{
List<int[]> temp = new ArrayList<int[]>();

for(int i = 0; i < list.size(); i++)//最高层,临时存放
{
if(list.get(i)[1] == father)
{
}
}

if(temp.size() < 1)
{
return result;
}
else//递归
{
for (int j = 0; j < list.size(); j++) //删除最高层
{
if (list.get(j)[1] == father)
{
list.remove(j);
}
}

for (int i = 0; i < temp.size() - 1; i++) //对最高层排序
{
for (int j = i + 1; j < temp.size(); j++)
{
if (temp.get(i)[2] > temp.get(j)[2])
{
int [] t = temp.get(i);
temp.set(i, temp.get(j));
temp.set(j, t);
}
}
}

for (int i = 0; i < temp.size(); i++)
{