comparator

Java - Sort Strings like Windows Explorer

百般思念 提交于 2019-12-28 03:03:47
问题 I am trying to use code suggested by Sander Pham on another question. I need my java ArrayList of string names to be sorted like Windows Explorer does. His code worked for everything but for one issue. I would have liked to comment onto that question, but I need more reputation points to comment. Anyways... He suggested to use a custom comparator implemented class and use that to compare the string names. Here is the code of that class: class IntuitiveStringComparator implements Comparator

Having trouble implementing a nested class comparator

余生颓废 提交于 2019-12-25 18:54:46
问题 I'm implementing a Point class and trying to use a nested class to implement a comparator, which will compare based on the slope of two points. I'm having trouble implementing such comparator and don't understand how to use it in my main() function. This is the error message I got when I try to compile it: Point.java:20: error: non-static variable this cannot be referenced from a static context if (Point.this.slopeTo(pt1) > Point.this.slopeTo(pt2)) { ^ Point.java:20: error: non-static

Implementing a Deck of Cards in Java

跟風遠走 提交于 2019-12-25 12:25:14
问题 So I have a lab (we are allowed to seek outside help on it, so here I am after lots of head scratching) where we have to implement a deck of cards. We have to use the enum class to create num For Suits: public enum Suits { CLUBS, HEARTS, DIAMONDS, SPADES } For Numerals: public enum Numerals { DEUCE(2), THREE(3), FOUR(4), FIVE(5), SIX(6), SEVEN(7), EIGHT(8), NINE(9), TEN(10), JACK(11), QUEEN(12), KING(13), ACE(14); } My card class is pretty straightforward, but I'm not sure about these two

I need to Split a string based on a complex delimiter

爱⌒轻易说出口 提交于 2019-12-25 06:53:14
问题 In C# I need to split a string (a log4j log file) into array elements based on a particular sequence of characters, namely "nnnn-nn-nn nn:nn:nn INFO". I'm currently splitting this log file up by newlines, which is fine except when the log statements themselves contain newlines. I don't control the input (the log file) so escaping them somehow is not an option. It seems like I should be able to use a comparator or a regex to identify the strings, but String.Split does not have an option like

Trying to sort a priority queue by one of the stored nodes' values

随声附和 提交于 2019-12-25 03:25:00
问题 So I'm new to priority queues. In an algorithm I'm trying to implement, I want to sort a priority queue according to the stored nodes' functionValue. I don't understand how the priority queue will know to sort the nodes by that value, as opposed to one of the other eight instance variables of my node objects. I'm pretty sure I define a Comparator object to define the comparison/ sorting rules, but I can't make heads or tails of the Oracle class library for Comparator. Here are the properties

Sorting list having itemBean by date?

寵の児 提交于 2019-12-25 01:33:53
问题 how to sort a list by date in android List<ItemBean> listIB = new ArrayList<ItemBean>(); for(int i=0; i<DispLibActivity.itemListVect.size(); i++) { listIB.add(DispLibActivity.itemListVect.get(i)); } ItemBean class have a member date public class ItemBean implements Parcelable{ String item_id, item_title, image_url, link_url, description, publish_date, in_app_date, sub_section_id, type, duration, orig_url, sync; .... .... } I want to sort list "listIB" with its member "publish_date" 回答1: use

sorting by date an array of bean having date member

只谈情不闲聊 提交于 2019-12-24 23:24:16
问题 i am having a bean class like public class ItemBean{ String item_id, item_title, image_url, link_url, description, publish_date, in_app_date, sub_section_id, type, duration, orig_url, sync; public ItemBean() { .... } .... .... } now in activity, i am getting values of itemBeans and making list; to use in comparator. You can see my code; public void sortDate() { List<ItemBean> listIB = new ArrayList<ItemBean>(); for(int i=0; i<DispLibActivity.itemListVect.size(); i++) { listIB.add

Fast(er) comparison of longs in byte format?

末鹿安然 提交于 2019-12-24 15:01:38
问题 I have a key value store that has byte[] keys. Some of these keys will be used for longs (longs themselves, also Localdate and LocalTime instances). I have a nice clean comparator, using standard Java and Guava: @Override public int compare(byte[] left, byte[] right) { long leftLong = Longs.fromByteArray(left); long rightLong = Longs.fromByteArray(right); return Long.compare(leftLong, rightLong); } but it's slower than I would expect. Sorting 100,000 longs takes 100ms, whereas sorting 100,000

Sort a list of Doubles with custom comparator for Double.NaN

我的梦境 提交于 2019-12-24 08:25:01
问题 I have the following list scala> List(Double.NaN, 0.0, 99.9, 34.2, 10.98, 7.0, 6.0, Double.NaN, 5.0, 2.0, 0.56, Double.NaN, 0.0, 10.0) res0: List[Double] = List(NaN, 0.0, 99.9, 34.2, 10.98, 7.0, 6.0, NaN, 5.0, 2.0, 0.56, NaN, 0.0, 10.0) This is my comparator function : scala> def sortAscendingDouble(d1:Double, d2:Double) = { | if(d1.isNaN && !d2.isNaN) | d1 < d2 | else if(!d1.isNaN && d2.isNaN) | d2 < d1 | else d1< d2 | } sortAscendingDouble: (d1: Double, d2: Double)Boolean I am trying to use

Error in std::list::sort with custom comparator (expected primary-expression before ')' token)

落爺英雄遲暮 提交于 2019-12-24 05:52:23
问题 The title is the main question. The exact scenario (I am 'using namespace std;'): void SubstringMiner::sortByOccurrence(list<Substring *> & substring_list) { list::sort(substring_list.begin(), substring_list.end(), Substring::OccurrenceComparator); } This is the comparator definition: class Substring { // ... class OccurrenceComparator { public: bool operator() (Substring * a, Substring *b); } }; Implementation of the comparator is intuitive and trivial. I am also using a very similar