如何使我的排序方法按字母顺序排序字符串的链表 [英] How to make my sort method sorts in alphabetical order a linked list of string

查看:194
本文介绍了如何使我的排序方法按字母顺序排序字符串的链表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图按字母顺序排序java中的链接列表,但我的方法仅适用于整数。



我尝试了什么:



公共节点排序()

{



节点当前;

Node sortedList = null;

int count;



if(first == null)

抛出新的IllegalArgumentException(List is empty);



//创建一个遍历整个列表的循环。

for(int index = 1; index< size(); index ++)

{

//将unsorteNode变量重定向到列表中的第二个元素。

current = first.next;



//将扫描变量重新编号为索引。

count = index;



//创建一个循环请仔细阅读并交换价值。

while(count> 0&& current.value.compareTo(first.value)> 0)

{

sortedList = new Node(current.value,first);

first = first.next;

current = current.next;

count--;

}



//切换价值在正确的位置。

first.next = current;

}



//返回已排序清单。

返回sortedList;



}

解决方案

从这里开始: 对象排序示例的Java ArrayList(可比较和比较器) [ ^ ]

I am trying to sort in alphabetical order a Linked List in java but my method only works with integers.

What I have tried:

public Node sort()
{

Node current;
Node sortedList = null;
int count;

if(first == null)
throw new IllegalArgumentException("List is empty");

// Creating a loop that will go through the whole list.
for (int index = 1; index < size(); index++)
{
// Redirects the unsorteNode variable to the second element in the list.
current = first.next;

// Redriects the scan variable to index.
count = index;

// Creating a loop that will go thorught the list and swap values.
while (count > 0 && current.value.compareTo(first.value) > 0)
{
sortedList = new Node(current.value, first);
first = first.next;
current = current.next;
count--;
}

// Switches the values in its correct position.
first.next = current;
}

// Returns the sorted list.
return sortedList;

}

解决方案

Start here: Java ArrayList of Object Sort Example (Comparable And Comparator)[^]


这篇关于如何使我的排序方法按字母顺序排序字符串的链表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆