
02.06.2012, 23:56
|
|
sinistral
|
|
Регистрация: 28.03.2011
Сообщений: 5,418
|
|
Сообщение от cyber
|
|
мне именно он и нужен , тот который браузер использует когда вызвается sort
|
ArrayPrototype.cpp
Удачи
|
Код:
|
EncodedJSValue JSC_HOST_CALL arrayProtoFuncSort(ExecState* exec)
{
JSObject* thisObj = exec->hostThisValue().toObject(exec);
unsigned length = thisObj->get(exec, exec->propertyNames().length).toUInt32(exec);
if (!length || exec->hadException())
return JSValue::encode(thisObj);
JSValue function = exec->argument(0);
CallData callData;
CallType callType = getCallData(function, callData);
if (thisObj->classInfo() == &JSArray::s_info && !asArray(thisObj)->inSparseMode()) {
if (isNumericCompareFunction(exec, callType, callData))
asArray(thisObj)->sortNumeric(exec, function, callType, callData);
else if (callType != CallTypeNone)
asArray(thisObj)->sort(exec, function, callType, callData);
else
asArray(thisObj)->sort(exec);
return JSValue::encode(thisObj);
}
// "Min" sort. Not the fastest, but definitely less code than heapsort
// or quicksort, and much less swapping than bubblesort/insertionsort.
for (unsigned i = 0; i < length - 1; ++i) {
JSValue iObj = thisObj->get(exec, i);
if (exec->hadException())
return JSValue::encode(jsUndefined());
unsigned themin = i;
JSValue minObj = iObj;
for (unsigned j = i + 1; j < length; ++j) {
JSValue jObj = thisObj->get(exec, j);
if (exec->hadException())
return JSValue::encode(jsUndefined());
double compareResult;
if (jObj.isUndefined())
compareResult = 1; // don't check minObj because there's no need to differentiate == (0) from > (1)
else if (minObj.isUndefined())
compareResult = -1;
else if (callType != CallTypeNone) {
MarkedArgumentBuffer l;
l.append(jObj);
l.append(minObj);
compareResult = call(exec, function, callType, callData, jsUndefined(), l).toNumber(exec);
} else
compareResult = (jObj.toUStringInline(exec) < minObj.toUStringInline(exec)) ? -1 : 1;
if (compareResult < 0) {
themin = j;
minObj = jObj;
}
}
// Swap themin and i
if (themin > i) {
thisObj->methodTable()->putByIndex(thisObj, exec, i, minObj, true);
if (exec->hadException())
return JSValue::encode(jsUndefined());
thisObj->methodTable()->putByIndex(thisObj, exec, themin, iObj, true);
if (exec->hadException())
return JSValue::encode(jsUndefined());
}
}
return JSValue::encode(thisObj);
} |
|
|

03.06.2012, 00:01
|
 |
I am Student
|
|
Регистрация: 17.12.2011
Сообщений: 4,415
|
|
спасибо это мне на долго 
|
|

03.06.2012, 00:06
|
|
sinistral
|
|
Регистрация: 28.03.2011
Сообщений: 5,418
|
|
Сообщение от cyber
|
спасибо это мне на долго
|
Неа.
Этот комментарий даёт наводку.
|
Цитата:
|
// "Min" sort. Not the fastest, but definitely less code than heapsort
// or quicksort, and much less swapping than bubblesort/insertionsort
|
Похож же исходный код?
|
Код:
|
template <class Item>
void selection(Item a[],int l,int r){
for(int i=l;i<r;i++){
int min=i;
for(int j=i+1;j<r+1;j++){
if( a[j]< a[min])
min=j;
}
if(a[min]!=a[i]) /* эта проверка делается только в устойчивых реализациях */
exch(a[i],a[min]);
}
} |
Значит это Сортировка выбором.
|
|

03.06.2012, 00:28
|
 |
I am Student
|
|
Регистрация: 17.12.2011
Сообщений: 4,415
|
|
я это хотел немного по другой причине узнать , а не сам алгоритм=)
document.getElementById("bt_Block").childNodes
данный код возвращает object Nodelist и у меня возникла идея сделать так
var obj = document.getElementById("bt_Block").childNodes;
var s = [].sort;
s.call.sort (obj, func);
|
|

03.06.2012, 00:38
|
 |
''
|
|
Регистрация: 11.12.2011
Сообщений: 636
|
|
|
Сообщение от melky
|
|
Похож же исходный код?
|
Ага, похож: ни там ни там ничё непонятно 
|
|

03.06.2012, 00:40
|
 |
''
|
|
Регистрация: 11.12.2011
Сообщений: 636
|
|
|
Сообщение от cyber
|
|
данный код возвращает object Nodelist и у меня возникла идея сделать так
|
cyber,
Это делается так
var obj = document.getElementById("bt_Block").childNodes;
obj = Array.prototype.slice.call(obj);
|
|

03.06.2012, 00:42
|
 |
I am Student
|
|
Регистрация: 17.12.2011
Сообщений: 4,415
|
|
Сообщение от Раед
|
cyber,
Это делается так
var obj = document.getElementById("bt_Block").childNodes;
obj = Array.prototype.slice.call(obj);
|
спс, до прототипного программирования я еще не дошел=)
|
|

03.06.2012, 00:56
|
 |
I am Student
|
|
Регистрация: 17.12.2011
Сообщений: 4,415
|
|
я так понимаю такой фокус не прокатит избавить от текстовых узлов
<div class="bt_inp_Block" id="bt_Block" >
<input type="text" class="inp_Out_color" value='выбор цвета >>' disabled="disabled" />
<input type="button" value="Color" class="start_Button" >
</div>
<script>
function sortChild (obj){
if (obj.nodeType == 1)return obj;
}
var obj = document.getElementById("bt_Block").childNodes;
obj = Array.prototype.slice.call(obj);
alert(obj.sort(sortChild))
</script>
|
|

03.06.2012, 02:43
|
 |
''
|
|
Регистрация: 11.12.2011
Сообщений: 636
|
|
<div class="bt_inp_Block" id="bt_Block" >
<input type="text" class="inp_Out_color" value='выбор цвета >>' disabled="disabled" />
<input type="button" value="Color" class="start_Button" >
</div>
<script>
function sortChild (obj){
if (obj.nodeType == 1) return true;
return false
}
var obj = document.getElementById("bt_Block").childNodes;
obj = Array.prototype.filter.call(obj, sortChild);
alert(obj)
</script>
Последний раз редактировалось Раед, 03.06.2012 в 04:11.
|
|

03.06.2012, 03:50
|
 |
I am Student
|
|
Регистрация: 17.12.2011
Сообщений: 4,415
|
|
Сообщение от Раед
|
<div class="bt_inp_Block" id="bt_Block" >
<input type="text" class="inp_Out_color" value='выбор цвета >>' disabled="disabled" />
<input type="button" value="Color" class="start_Button" >
</div>
<script>
function sortChild (obj){
if (obj.nodeType == 1) return true;
return false
}
var obj = document.getElementById("bt_Block").childNodes;
obj = Array.prototype.filter.call(obj, sortChild);
alert(obj)
</script>
filter
|
это типо ссылка на стандартный метод?
спс что помогаете=)
Последний раз редактировалось cyber, 03.06.2012 в 05:03.
|
|
|
|