我需要编写一个Java Comparator类来比较Strings,但是有一个转折.如果它比较的两个字符串在字符串的开头和结尾是相同的,并且不同的中间部分是整数,则根据这些整数的数值进行比较.例如,我希望以下字符串以它们显示的顺序结束:
如您所见,字符串中可能还有其他整数,因此我不能只使用正则表达式来分解任何整数.我正在考虑从一开始就走绳子,直到找到一点不匹配,然后走到最后,直到找到一个不匹配的位,然后比较中间的位到正则表达式"[0-9] +",如果比较,则进行数值比较,否则进行词法比较.
有没有更好的办法?
更新我不认为我可以保证字符串中的其他数字,可能匹配的数字,周围没有空格,或者不同的数字确实有空格.
我有一个命令(cmd1),它通过一个日志文件来过滤掉一组数字.这些数字是随机顺序,因此我使用sort -gr来获得反向排序的数字列表.此排序列表中可能存在重复项.我需要找到该列表中每个唯一号码的计数.
例如,如果cmd1的输出是:
100
100
100
99
99
26
25
24
24
Run Code Online (Sandbox Code Playgroud)
我需要另一个命令,我可以管道上面的输出,所以,我得到:
100 3
99 2
26 1
25 1
24 2
Run Code Online (Sandbox Code Playgroud) 如果我有Map这样的:
HashMap<Integer, ComparableObject> map;
Run Code Online (Sandbox Code Playgroud)
我想获得一个使用自然排序排序的值集合,哪种方法最快?
创建可排序集合的实例,例如ArrayList,添加值,然后对其进行排序:
List<ComparableObject> sortedCollection = new ArrayList<ComparableObject>(map.values());
Collections.sort(sortedCollection);
Run Code Online (Sandbox Code Playgroud)
创建一个有序集合的实例TreeSet,然后添加值:
Set<ComparableObject> sortedCollection = new TreeSet<ComparableObject>(map.values());
Run Code Online (Sandbox Code Playgroud)
请注意,结果集合永远不会被修改,因此排序只需要进行一次.
我有一个Contact对象数组:
var contacts:[Contact] = [Contact]()
Run Code Online (Sandbox Code Playgroud)
联系班级:
Class Contact:NSOBject {
var firstName:String!
var lastName:String!
}
Run Code Online (Sandbox Code Playgroud)
我希望按顺序对该数组进行排序,lastName然后firstName在某些联系人得到相同的情况下lastName.
我可以按照其中一个标准排序,但不能同时排序.
contacts.sortInPlace({$0.lastName < $1.lastName})
Run Code Online (Sandbox Code Playgroud)
如何添加更多条件来对此数组进行排序?
我有2个列表对象,一个是一个int列表,另一个是对象列表,但对象有一个ID属性.
我想要做的是按照与int列表相同的排序顺序按其ID排序对象列表.
我已经玩了一段时间试图让它工作,到目前为止没有快乐,
这是我到目前为止...
//**************************
//*** Randomize the list ***
//**************************
if (Session["SearchResultsOrder"] != null)
{
// save the session as a int list
List<int> IDList = new List<int>((List<int>)Session["SearchResultsOrder"]);
// the saved list session exists, make sure the list is orded by this
foreach(var i in IDList)
{
SearchData.ReturnedSearchedMembers.OrderBy(x => x.ID == i);
}
}
else
{
// before any sorts randomize the results - this mixes it up a bit as before it would order the results by member …Run Code Online (Sandbox Code Playgroud) 我有一个集合:
List<VPair<Item, List<Item>> dependencyHierarchy;
Run Code Online (Sandbox Code Playgroud)
对中的第一项是某个对象(项),第二项是第一项依赖的相同类型对象的集合.我希望得到一个List<Item>依赖顺序,所以没有依赖于第一个元素的项目等等(没有循环依赖!).
输入:
Item4 depends on Item3 and Item5 Item3 depends on Item1 Item1 does not depend on any one Item2 depends on Item4 Item5 does not depend on any one
结果:
Item1 Item5 Item3 Item4 Item2
谢谢.
解:
拓扑排序(感谢LoïcFévrier的想法)
和
我有一个包含三个字段的"Report"对象列表(All String type) -
ReportKey
StudentNumber
School
Run Code Online (Sandbox Code Playgroud)
我有一个排序代码像 -
Collections.sort(reportList, new Comparator<Report>() {
@Override
public int compare(final Report record1, final Report record2) {
return (record1.getReportKey() + record1.getStudentNumber() + record1.getSchool())
.compareTo(record2.getReportKey() + record2.getStudentNumber() + record2.getSchool());
}
});
Run Code Online (Sandbox Code Playgroud)
出于某种原因,我没有排序顺序.建议在场之间放置空格,但为什么呢?
你看到代码有什么问题吗?
我在排序数组wrt数据库时遇到问题:
NSSortDescriptor *sorter = [[NSSortDescriptor alloc] initWithKey:@"w" ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObject: sorter];
[mGlossaryArray sortUsingDescriptors:sortDescriptors];
[sorter release];
Run Code Online (Sandbox Code Playgroud)
在数据库中有一些第一个大写字母,因为大写字母它没有显示我正确的排序输出.这里我用rt"w"排序一个数组,这是我在数据库中的表列.在这里,我附加了输出的屏幕截图,其中"癌症"首先出现在"c"之后,但这不正确,因为大写单词而没有按字母顺序排序.
例如.如果小写中有"able"和"aCid"那么它将首先显示aCid然后能够显示aCid,并且还存在如果第一个字母是大写字母它首先出现的情况,例如"Able"和"a".在这里Able首先显示.
如何找到唯一的行并从文件中删除所有重复项?我的输入文件是
1
1
2
3
5
5
7
7
Run Code Online (Sandbox Code Playgroud)
我希望结果如下:
2
3
Run Code Online (Sandbox Code Playgroud)
sort file | uniq不会做这个工作.将显示所有值1次
我正在使用nltk语料库movie_reviews,其中有很多文档.我的任务是通过预处理数据获得这些评论的预测性能,而无需预处理.但是有问题,在列表documents和documents2我有同样的文件,我需要他们打乱,为了保持相同的顺序在这两个列表.我不能单独洗牌,因为每次我洗牌都会得到其他结果.这就是为什么我需要以相同的顺序对其进行一次洗牌,因为我需要在最后比较它们(这取决于顺序).我正在使用python 2.7
示例(实际上是字符串标记化,但它不是相对的):
documents = [(['plot : two teen couples go to a church party , '], 'neg'),
(['drink and then drive . '], 'pos'),
(['they get into an accident . '], 'neg'),
(['one of the guys dies'], 'neg')]
documents2 = [(['plot two teen couples church party'], 'neg'),
(['drink then drive . '], 'pos'),
(['they get accident . '], 'neg'),
(['one guys dies'], 'neg')]
Run Code Online (Sandbox Code Playgroud)
我需要在洗牌后得到这个结果:
documents = [(['one of the guys dies'], 'neg'),
(['they get into …Run Code Online (Sandbox Code Playgroud) sorting ×10
java ×3
algorithm ×2
c# ×2
collections ×2
list ×2
bash ×1
command-line ×1
comparison ×1
count ×1
dependencies ×1
duplicates ×1
iphone ×1
linq ×1
linux ×1
nsarray ×1
python ×1
shuffle ×1
string ×1
swift ×1
uniq ×1
unique ×1