compareto

How to sort without considering special characters in java?

天涯浪子 提交于 2021-02-05 10:00:50
问题 I wanted to sort a list of names and I used compareTo method and I have names like this on my list $urya, andy, prince, @rikesh, expected output: andy, prince, $urya, @rikesh, instead, I get: $urya @rikesh andy prince What happens now is the name that starts with special characters come's first and then other names are sorted Is there a way to push the names that starts with special characters to last. if (this.firstName == name.firstName) { return 0; } else if (this.firstName == null) {

compareTo Error: Cannot invoke compareTo(int) on the primitive type int

纵饮孤独 提交于 2021-02-05 07:57:12
问题 Not sure how to get the compareTo to not give error. Very lost. Tried changing multiples lines of code. public voide addValue(int newNumber){ int index = 0; while ((index < numbers.size()) && (numbers.get(index.compareTo(newNumber) ==-1)) index++; numbers.add(index, NewNumber); } I expected it to be able to have no issues with compareTo(newNumber) . Error Message: "Cannot invoke compareTo(int) on the primitive type int" 回答1: Edit: I can't believe what I originally wrote here. Can you not just

Cannot find compareTo when receiving comparable Object[]

99封情书 提交于 2021-01-25 07:19:08
问题 The problem is the method is not sure a and b are comparable so the compiler throws a cannot find symbol for compareTo. public class Merge { public ArrayList<Object> sorted(Object[] a, Object[] b) { int i, j, k; i = j = k = 0; ArrayList<Object> c = new ArrayList<>(); while (i < a.length || j < b.length) { if (i == a.length) { for (j = j; j < b.length; j++) { c.add(k, b[j]); k++; } break; } else if (j == b.length) { for (i = i; i < a.length; i++) { c.add(k, a[i]); k++; } break; } else { if ( a

Cannot find compareTo when receiving comparable Object[]

╄→尐↘猪︶ㄣ 提交于 2021-01-25 07:19:05
问题 The problem is the method is not sure a and b are comparable so the compiler throws a cannot find symbol for compareTo. public class Merge { public ArrayList<Object> sorted(Object[] a, Object[] b) { int i, j, k; i = j = k = 0; ArrayList<Object> c = new ArrayList<>(); while (i < a.length || j < b.length) { if (i == a.length) { for (j = j; j < b.length; j++) { c.add(k, b[j]); k++; } break; } else if (j == b.length) { for (i = i; i < a.length; i++) { c.add(k, a[i]); k++; } break; } else { if ( a

Using compareToIgnoreCase() to compare an entry to substring in string without using array

夙愿已清 提交于 2020-12-26 12:24:57
问题 In my assignment it's forbidden to use collections and any arrays. We are allowed to use String Tokenizer but any other classes than String and System are not allowed. This solution has to work with any number of entries. I have a string which look like this : 1|Aaron|Peter|3063543030|john@gmail.com + "\n" 2|Buffet|Anthony|3063543030|john@gmail.com + "\n" 3|Dunty|Richard|3063543030|john@gmail.com For example, if the entry is 4|Doe|John|3063543030|john@gmail.com then the comparison will be

Using compareToIgnoreCase() to compare an entry to substring in string without using array

折月煮酒 提交于 2020-12-26 12:24:06
问题 In my assignment it's forbidden to use collections and any arrays. We are allowed to use String Tokenizer but any other classes than String and System are not allowed. This solution has to work with any number of entries. I have a string which look like this : 1|Aaron|Peter|3063543030|john@gmail.com + "\n" 2|Buffet|Anthony|3063543030|john@gmail.com + "\n" 3|Dunty|Richard|3063543030|john@gmail.com For example, if the entry is 4|Doe|John|3063543030|john@gmail.com then the comparison will be

cannot be cast to [Ljava.lang.Comparable

天涯浪子 提交于 2020-06-12 08:44:06
问题 So I need to do dynamic ordered list. public class DynArrayListOrd<T extends Comparable<T>> { private T[] tab ; public DynArrayListOrd() { tab = (T[])new Object[startSize]; } .... main { DynArrayListOrd tab = new DynArrayListOrd(); tab.add("John"); tab.add("Steve"); } And when I run the code I get error: Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.Comparable; at structures.DynArrayListOrd.<init>(DynArrayListOrd.java:14) at

How can I sort a string with integers in Java?

丶灬走出姿态 提交于 2020-05-23 10:36:31
问题 I have got an array. Each space in my array holds two strings (one string contains just letters, the other one numbers). What I am trying to do now is to sort the array either alphabetically or numerically (depending on which space in the array is chosen). To do that, I am using the compareTo() method. However, I found that when I try to sort the array according to the numbers, it actually does not really work. My guess is, that since Java handles strings with the ascii codes, numbers don't

《Effective JAVA学习笔记》之 compareTo()

南楼画角 提交于 2020-02-29 11:13:48
class Person implements Comparable<Person> { String firstName; String lastName; int birthdate; // Compare by firstName, break ties by lastName, finally break ties by birthdate public int compareTo(Person other) { if (firstName.compareTo(other.firstName) != 0) return firstName.compareTo(other.firstName); else if (lastName.compareTo(other.lastName) != 0) return lastName.compareTo(other.lastName); else if (birthdate < other.birthdate) return -1; else if (birthdate > other.birthdate) return 1; else return 0; } } 总是实现泛型版本 Comparable 而不是实现原始类型 Comparable 。因为这样可以节省代码量和减少不必要的麻烦。 只关心返回结果的正负号(负/零/正)

Why do we need List to sort using Collection.sort() method?

好久不见. 提交于 2020-02-05 03:27:30
问题 I am planning to sort keys from a hashmap. I am using a customized sort method. Following code gives me compile-time error on compareTo() method where I am using Set as Collection Set<String> set = map.keySet(); Collections.sort(set, (a, b) -> map.get(a) == map.get(b) ? a.compareTo(b) : map.get(b) - map.get(a)); If I convert Set to List and then sort then everything works fine . List<String> words = new ArrayList<>(map.keySet()); Collections.sort(words, (a, b) -> map.get(a) == map.get(b) ? a