English 中文(简体)
我怎么能够划一阵列,但排除某些内容(将保持阵列中的相同位置)
原标题:How can I sort an array, yet exclude certain elements (to be kept at the same position in the array)

将在Javascript (jQuery)中实施,但我相信,这种方法可以用任何语言使用。

我有各种各样的项目,我需要做这样的工作。 <>光> 阵列中有一些项目必须保持同一地位(同级指数)。

所涉阵列是从<代码><li>元素和Im清单中选取的,使用该清单项目的所附数据()值作为分类值。

在这方面最好采取什么做法?

<ul id="fruit">
  <li class="stay">bananas</li>
  <li>oranges</li>
  <li>pears</li>
  <li>apples</li>
  <li class="stay">grapes</li>
  <li>pineapples</li>
</ul>

<script type="text/javascript">
    var sugarcontent = new Array( 32 , 21 , 11 , 45 , 8 , 99 );
    $( #fruit li ).each(function(i,e){
       $(this).data( sugar ,sugarcontent[i]);
    })
</script>

我希望清单能够得出以下结果。

<ul id="fruit">
      <li class="stay">bananas</li> <!-- score = 32 -->
      <li>pineapples</li> <!-- score = 99 -->
      <li>apples</li> <!-- score = 45 -->
      <li>oranges</li> <!-- score = 21 -->
      <li class="stay">grapes</li> <!-- score = 8 -->
      <li>pears</li> <!-- score = 11 -->
  </ul>

感谢!

最佳回答

1. 具有以下特点:

  • Extract and sort items not marked with stay
  • Merge stay 物项和分类项目

    var sugarcontent = new Array(32, 21, 11, 45, 8, 99);
    
    var items = $( #fruit li );
    
    items.each(function (i) {
        $(this).data( sugar , sugarcontent[i]);
        // Show sugar amount in each item text - for debugging purposes
        if ($(this).hasClass( stay ))
            $(this).text("s " + $(this).text());
        else
            $(this).text(sugarcontent[i] + " " + $(this).text());
    });
    
    // Sort sortable items
    var sorted = $(items).filter( :not(.stay) ).sort(function (l, r) {
        return $(l).data( sugar ) - $(r).data( sugar );
    });
    
    // Merge stay items and sorted items
    var result = [];
    var sortedIndex = 0;
    
    for (var i = 0; i < items.length; i++)
        if (!$(items[i]).hasClass( stay )) {
            result.push(sorted[sortedIndex]);
            sortedIndex++;
        }
        else
            result.push(items[i]);
    
    // Show result
    $( #fruit ).append(result);
    
问题回答

应:

var sugarcontent = new Array( 32 , 21 , 11 , 45 , 8 , 99 );
var list = $( #fruit );
var lis = list.find( li ).each(function(i,e){
   $(this).data( score ,sugarcontent[i]);
});
var stay = lis.filter( .stay ).each(function(){
    $(this).data( index ,$(this).index());
});
lis.sort(function(a,b){
    return $(b).data( score ) - $(a).data( score );
}).appendTo(list);
stay.each(function(){
    var index = $(this).data( index );
    if (index == 0) {
        list.prepend(this);
    } else {
        lis.filter( :eq( +index+ ) ).insertAfter(this);
    }
}

这样,这些物品的指数与班级的停留相加,然后用记分取,然后用班级在正确的地点停留。

你认为解决办法是通用的,适用于任何发展环境。

你们需要把你的清单分成两个不同的清单,即需要分类的清单和有待确定的清单。 然后,将第一个清单分类,与第二个清单合并。

你再次面临的关键问题是: 大多数类型的算法(包括,这是大多数框架中最常见的算法),如果你的比较职能依赖于任何外部国家(如项目立场),就会变得非常不端。

正如Bevan所指出的,这赢得了工作,但为了教育目的,我将离开这里:

$( #fruit li ).sort(function(a, b) {
    return ($(a).hasClass( stay ) || $(b).hasClass( stay ))
        ? 0 : (a.data( sugar ) > b.data( sugar ) ? 1 : -1);
}).appendTo( #fruit );

注:你需要用甘蔗确定甘蔗数据,以说明:

.data( sugar , sugarcontent[i]);




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签