compare

How to know if two arrays have the same values

情到浓时终转凉″ 提交于 2019-11-26 14:30:01
I have these two arrays: one is filled with information from an ajax request and another stores the buttons the user clicks on. I use this code (I filled with sample numbers): var array1 = [2, 4]; var array2 = [4, 2]; //It cames from the user button clicks, so it might be disordered. array1.sort(); //Sorts both Ajax and user info. array2.sort(); if (array1==array2) { doSomething(); }else{ doAnotherThing(); } But it always gives false , even if the two arrays are the same, but with different name. (I checked this in Chrome's JS Console). So, is there any way I could know if these two arrays

Compare two arrays of primitives in Java?

删除回忆录丶 提交于 2019-11-26 14:28:05
问题 I know about Arrays.deepEquals(Object[], Object[]) but this doesn't work for primitive types (due limitations of arrays and autoboxing, see this related post). With that in mind, is this the most efficient approach? boolean byteArrayEquals(byte[] a, byte[] b) { if (a == null && b == null) return true; if (a == null || b == null) return false; if (a.length != b.length) return false; for (int i = 0; i < a.length; i++) { if (a[i] != b[i]) return false; } return true; } 回答1: Change your first

Easiest way to compare arrays in C#

拈花ヽ惹草 提交于 2019-11-26 14:25:57
In Java, Arrays.equals() allows to easily compare the content of two basic arrays (overloads are available for all the basic types). Is there such a thing in C#? Is there any "magic" way of comparing the content of two arrays in C#? You could use SequenceEqual . This works for any IEnumerable<T> , not just arrays. John Buchanan Use SequenceEqual in LINQ . int[] arr1 = new int[] { 1,2,3}; int[] arr2 = new int[] { 3,2,1 }; Console.WriteLine(arr1.SequenceEqual(arr2)); // false Console.WriteLine(arr1.Reverse().SequenceEqual(arr2)); // true Also for arrays (and tuples) you can use new interfaces

Comparing two integer arrays in java

久未见 提交于 2019-11-26 13:44:10
I am trying to write code to compare two arrays. In the first array I have put my own digits, but the second the array takes numbers from the input file. The size of this array is determined by the first number in the file while the first array is always of size 10. The length must be the same for both arrays as well as the numbers. My code is below: public static void compareArrays(int[] array1, int[] array2) { boolean b = false; for (int i = 0; i < array2.length; i++) { for (int a = 0; a < array1.length; a++) { if (array2[i] == array1[a]) { b = true; System.out.println("true"); } else { b =

How would you compare jQuery objects?

寵の児 提交于 2019-11-26 13:03:28
So I'm trying to figure out how to compare two jQuery objects, to see if the parent element is the body of a page. here's what I have: if ( $(this).parent() === $('body') ) ... I know this is wrong, but if anybody understands what I'm getting at, could they point me towards the correct way of doing this? You need to compare the raw DOM elements, e.g.: if ($(this).parent().get(0) === $('body').get(0)) or if ($(this).parent()[0] === $('body')[0]) Why not: if ($(this).parent().is("body")) { ... } ? Looping is not required, testing the single first node is not required. Pretty much nothing is

Using jQuery to compare two arrays of Javascript objects

白昼怎懂夜的黑 提交于 2019-11-26 12:44:00
I have two arrays of JavaScript Objects that I'd like to compare to see if they are the same. The objects may not (and most likely will not) be in the same order in each array. Each array shouldn't have any more than 10 objects. I thought jQuery might have an elegant solution to this problem, but I wasn't able to find much online. I know that a brute nested $.each(array, function(){}) solution could work, but is there any built in function that I'm not aware of? Thanks. Sudhakar R There is an easy way... $(arr1).not(arr2).length === 0 && $(arr2).not(arr1).length === 0 If the above returns true

165. Compare Version Numbers

大城市里の小女人 提交于 2019-11-26 12:41:24
今天项目里遇到以"." 、""、“|”分割字符串,直接用"." 、""、“|”无法分割,因为"." 、""、“|”是特殊字符,需要转义,"\." 、"\"、“\|”。 class Solution { public int compareVersion(String version1, String version2) { String[] arr1 = version1.split("\\."); String[] arr2 = version2.split("\\."); int n = arr1.length>arr2.length?arr1.length:arr2.length; int temp1[] = new int [n]; int temp2[] = new int [n]; for(int i = 0 ; i < arr1.length ; i++) { temp1[i] = Integer.parseInt(arr1[i]); } for(int i = 0 ; i < arr2.length ; i++) { temp2[i] = Integer.parseInt(arr2[i]); } int flag = 0; while(flag<n) { if(temp1[flag]<temp2[flag]) return -1; else if(temp1

How to compare Unicode strings in Javascript?

陌路散爱 提交于 2019-11-26 12:38:02
问题 When I wrote in JavaScript \"Ł\" > \"Z\" it returns true . In Unicode order it should be of course false . How to fix this? My site is using UTF-8. 回答1: You can use Intl.Collator or String.prototype.localeCompare, introduced by ECMAScript Internationalization API: "Ł".localeCompare("Z", "pl"); // -1 new Intl.Collator("pl").compare("Ł","Z"); // -1 -1 means that Ł comes before Z , like you want. Note it only works on latest browsers, though. 回答2: Here is an example for the french alphabet that

Java compare方法和compareTo方法

喜欢而已 提交于 2019-11-26 12:28:37
Java Comparator接口排序用法,详细介绍可以阅读这个链接的内容: https://www.cnblogs.com/shizhijie/p/7657049.html 对于 public int compare(Object arg0, Object arg1)的理解 1 //数组排序 2 String[] str = new String[5]; 3 Arrays.sort(str, new Comparator<String>() { 4 @Override 5 public int compare(String o1, String o2) { 6 // TODO Auto-generated method stub 7 return 0; 8 } 9 }); 10 //List集合排序 11 ArrayList<String> list = new ArrayList<String>(); 12 Collections.sort(list, new Comparator<String>() { 13 @Override 14 public int compare(String o1, String o2) { 15 // TODO Auto-generated method stub 16 return 0; 17 } 18 });

Text comparison algorithm

不打扰是莪最后的温柔 提交于 2019-11-26 12:24:20
问题 We have a requirement in the project that we have to compare two texts (update1, update2) and come up with an algorithm to define how many words and how many sentences have changed. Are there any algorithms that I can use? I am not even looking for code. If I know the algorithm, I can code it in Java. 回答1: Typically this is accomplished by finding the Longest Common Subsequence (commonly called the LCS problem). This is how tools like diff work. Of course, diff is a line-oriented tool, and it