Javascript-форум (https://javascript.ru/forum/)
-   Events/DOM/Window (https://javascript.ru/forum/events/)
-   -   помогите понять (https://javascript.ru/forum/events/28787-pomogite-ponyat.html)

melky 02.06.2012 23:56

Цитата:

Сообщение от cyber (Сообщение 178374)
мне именно он и нужен , тот который браузер использует когда вызвается 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);
}


cyber 03.06.2012 00:01

спасибо это мне на долго :blink:

melky 03.06.2012 00:06

Цитата:

Сообщение от cyber (Сообщение 178377)
спасибо это мне на долго :blink:

Неа.

Этот комментарий даёт наводку.
Цитата:

// "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]);
    }
}

Значит это Сортировка выбором.

cyber 03.06.2012 00:28

я это хотел немного по другой причине узнать , а не сам алгоритм=)
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

Цитата:

Сообщение от melky
Похож же исходный код?

Ага, похож: ни там ни там ничё непонятно :D

Раед 03.06.2012 00:40

Цитата:

Сообщение от cyber
данный код возвращает object Nodelist и у меня возникла идея сделать так

cyber,
Это делается так
var obj = document.getElementById("bt_Block").childNodes;
obj = Array.prototype.slice.call(obj);

cyber 03.06.2012 00:42

Цитата:

Сообщение от Раед (Сообщение 178386)
cyber,
Это делается так
var obj = document.getElementById("bt_Block").childNodes;
obj = Array.prototype.slice.call(obj);

спс, до прототипного программирования я еще не дошел=)

cyber 03.06.2012 00:56

я так понимаю такой фокус не прокатит избавить от текстовых узлов
<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

<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>

cyber 03.06.2012 03:50

Цитата:

Сообщение от Раед (Сообщение 178397)
<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

это типо ссылка на стандартный метод?
спс что помогаете=)


Часовой пояс GMT +3, время: 08:28.