compare

Array的sort() 方法

牧云@^-^@ 提交于 2019-11-28 10:06:42
参考JavaScript高级程序设计 在默认情况下,sort()方法按升序排列数组项--即最小的值位于最前面,最大的值排在最后面。为了实现排序,sort方法会调用每个数组项的toSting()转型方法,然后比较得到的字符串,以确定如何排序。即使数组中每一项都是数值,sort()方法 比较的也是字符串 ,如下所示: var values = [0,1,5,10,15]; values.sort(); alert(values); //0,1,10,15,5 可见,例子中的数组中值顺序没有问题,但sort()方法会根据字符串测试结果改变原来的顺序。因为即使数值5小于10,但在进行字符串比较时,“10”位于“5”前面,于是数组的顺序被修改了。 所以,这种排序方式在很多情况下都不是最佳方案。 因此sort()方法可以接收一个比较函数作为参数,以便我们指定哪个值位于哪个值的前面。 比较函数接收两个参数,如果第一个参数应该位于第二个之前则 返回一个负数 ,如果两个参数相等则返回0,如果第一个参数应该位于第二个参数之后则 返回一个正数 。以下是一个简单的比较函数: function compare(value1,value2) { if (value1 < value2) { return -1; } else if (value1 > value2) { return 1; } else {

KeyNotFoundException in filled dictionary

拥有回忆 提交于 2019-11-28 10:02:03
问题 I am trying to modify value in dictionary, but the compiler throws KeyNotFoundException . I'm sure, I declared that key in dictionary, because I am calling GenerateEmptyChunks() method, which fills dictionary with chunks with key of their position and values are empty for level generator. I've checked debugger and Chunks dictionary object is correctly filled with keys and values. Is it caused by my unworking CompareTo method? If yes, how I have modify CompareTo method to return right values?

What exactly does comparing Integers with == do?

被刻印的时光 ゝ 提交于 2019-11-28 10:01:15
EDIT: OK, OK, I misread. I'm not comparing an int to an Integer. Duly noted. My SCJP book says: When == is used to compare a primitive to a wrapper, the wrapper will be unwrapped and the comparison will be primitive to primitive. So you'd think this code would print true : Integer i1 = 1; //if this were int it'd be correct and behave as the book says. Integer i2 = new Integer(1); System.out.println(i1 == i2); but it prints false . Also, according to my book, this should print true : Integer i1 = 1000; //it does print `true` with i1 = 1000, but not i1 = 1, and one of the answers explained why.

jQuery compare two DOM object?

倖福魔咒の 提交于 2019-11-28 09:59:25
Clicking on an element: $('.my_list').click(function(){ var selected_object = $(this); $('.my_list').each(function(){ var current_object = $(this); if( selected_object == current_object ) alert('FOUND IT !'); }); }); I don't know why, but I don't get the alert message "FOUND IT !". You can use the jQuery.is function: Check the current matched set of elements against a selector, element, or jQuery object and return true if at least one of these elements matches the given arguments. if (selected_object.is(current_object)) { ... } An alternate solution is to use jQuery.get function to get the raw

How to compare strings

守給你的承諾、 提交于 2019-11-28 09:39:52
I wanted to compare a string without actually defining one of them as a string, something like this, if (string == 'add') Do I have to declare 'add' as a string or is it possible to compare in a similar way? In C++ the std::string class implements the comparison operators , so you can perform the comparison using == just as you would expect: if (string == "add") { ... } When used properly, operator overloading is an excellent C++ feature. You need to use strcmp . if (strcmp(string,"add") == 0){ print("success!"); } Algorithmist You could use strcmp() : /* strcmp example */ #include <stdio.h>

Php compare strings and return common values

亡梦爱人 提交于 2019-11-28 09:13:24
问题 I have tried to find other posts/information on this and none of them seem to work - although I'm sure this is a simple task. I have two strings, and I would like to have some lines of code that give me the word that they have in common. For example, I may have... String1 = "Product Name - Blue"; String2 = "Blue Green Pink Black Orange"; And I would like to have a string only containing the value Blue. How can I do this? Thanks in advance! 回答1: You can use explode and array_intersect maybe?

comparing arrays in objective-c

佐手、 提交于 2019-11-28 09:12:38
Ok a pretty simple question.. in c++ it seems to work but in objective-c i seem to struggle with it :S .. If you want to compare two arrays it should be something like this right for ( int i = 0; i < [appdelegate.nicearray count]; i++ ) { if ( appdelegate.nicearray[i] == appdelegate.exercarray[i] ) { NSLog(@"the same elements in this selection"); } } what's the problem exactly ? These are Cocoa array objects (instances of NSArray), not C arrays or C++ vectors, and remember that Objective-C does not have operator overloading. The only things you can do with an object are pass it around, store

Beyond Compare 4 密钥被吊销

試著忘記壹切 提交于 2019-11-28 08:28:41
Beyond Compare 4用一段时间后就会弹出这个: 因为Beyond Compare会定期联网验证你的密钥是否有效。 网上查找的解决方案基本是2步: 1、禁止Beyond Compare联网,配置防火墙出站规则就行( http://www.xz7.com/downinfo/280960.html ) 2、删除BCState.xml,路径:C:\Users\xxx\AppData\Roaming\Scooter Software\Beyond Compare 4,xxx是当前登录用户名 但是最近不知道出了什么问题,明明禁止联网了,还是弹出密钥被吊销。。。 于是我对比了吊销前后的BCState.xml文件,发现多了一个节点<TCheckForUpdatesState>: 经过测试,只要删除该节点或子节点<CheckID>就没问题了。 如果直接删除BCState.xml整个文件,那在启动界面就不会显示自动保存的对比记录。 另外,子节点<LastChecked>顾名思义是记录最近一次验证时间,猜测一下,是否把值调到一个很大的时间,就不会再次校验了呢?(经验证,并不是) 来源: https://www.cnblogs.com/linys2333/p/11082353.html

PHP compare two arrays and get the matched values not the difference

久未见 提交于 2019-11-28 08:07:58
I'm trying to compare two arrays and get only the values that exist on both arrays but, unfortunately, I can't find the right array function to use... I found the array_diff() function: http://php.net/manual/en/function.array-diff.php But it's for the difference of the both arrays. Example: $array1 = array("**alpha**","omega","**bravo**","**charlie**","**delta**","**foxfrot**"); $array2 = array("**alpha**","gamma","**bravo**","x-ray","**charlie**","**delta**","halo","eagle","**foxfrot**"); Expected Output: $result = array("**alpha**","**bravo**","**charlie**","**delta**","**foxfrot**"); Simple

Is it correct to compare two rounded floating point numbers using the == operator?

纵饮孤独 提交于 2019-11-28 07:49:57
问题 Or is there a chance that the operation will fail? Thanks. I chose the wrong term and what I really meant was rounding to 0, not truncation. The point is, I need to compare the integer part of two doubles and I'm just casting them to int and then using ==, but, as someone pointed out in one of my earlier questions, this could throw an overflow exception if the double can't fit into the integer. So the question would be 'Is it correct to use the == operator to compare two doubles that have