Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sorting 使用日期不起作用的嵌套div对项目标记进行排序_Sorting_Date_Text_Article - Fatal编程技术网

Sorting 使用日期不起作用的嵌套div对项目标记进行排序

Sorting 使用日期不起作用的嵌套div对项目标记进行排序,sorting,date,text,article,Sorting,Date,Text,Article,我一直在尝试对日期文本和链接进行排序。有了这个问题的帮助,我真的走了很远。此时,日期被读取为文本字符串,而不是日期字符串。我只需要弄清楚如何将文本转换成日期字符串。任何帮助都将不胜感激 `<script type='text/javascript'> $(document).ready(function() { function sortUsingNestedText(parent, childSelector, keySelector

我一直在尝试对日期文本和链接进行排序。有了这个问题的帮助,我真的走了很远。此时,日期被读取为文本字符串,而不是日期字符串。我只需要弄清楚如何将文本转换成日期字符串。任何帮助都将不胜感激

        `<script type='text/javascript'>
        $(document).ready(function() {
        function sortUsingNestedText(parent, childSelector, keySelector, caller) {
        var articles;

        var items = parent.children(childSelector).sort(function(a, b) {
            var vA = $(keySelector, a).text();
            var vB = $(keySelector, b).text();
            return (vA < vB) ? -1 : (vA > vB) ? 1 : 0;
        });

        var articles = $();
        items.each(function() {
            if ($(this).text().indexOf("articles") != -1) { //if it contains "articles"
                articles = articles.add($(this)); //adds to the articles jQuery object
                items = items.not($(this)); //and remove from items
            }
        });

        if (caller.data('order_by') == 'asc') {
            articles = articles.sort(function(a, b) {
                var vA = $(a).text();
                var vB = $(b).text();
                return (vA < vB) ? -1 : (vA > vB) ? 1 : 0;
            });
        }

        if (caller.data('order_by') == 'desc') { //alert('desc pattern');
            caller.data('order_by', 'asc');
            parent.append(items.get().reverse());
            parent.append(articles.get().reverse());
        } else { //alert('asc pattern');
            caller.data('order_by', 'desc');
            parent.append(articles);
            parent.append(items);
            }
        }

        $('#date').data("sortKey", "div.article-preview-date").data('order_by', 'desc');
        $('#name').data("sortKey", "div.article-preview").data('order_by', 'desc');

        $("#date").click(function() {
         $('.article-preview-left-col > .article-preview-date > span').contents().unwrap();
         $('.blog-year-month').remove();
         sortUsingNestedText($('#sort'), "article", $("#date").data("sortKey"), $(this));

        });

        $("#name").click(function() {
        $('.article-preview-left-col > .article-preview-date > span').contents().unwrap();
        $('.blog-year-month').remove();
        sortUsingNestedText($('#sort'), "article", $("#name").data("sortKey"), $(this));
        });


        }) 

        </script>
        </head>
        <body>


      <ul id="date-title-toggle">
        <li class="list-alpha" id="date"><a href="#">Date</a></li>
        <li class="list-omega" id="name"><a href="#">Title</a></li>
      </ul>


    <section id="sort">
       <span class="blog-year-month">2012</span>
      <article class="articles">
        <div class="article-preview-left-col">
          <div class="article-preview-date"> <span class="month">January</span> <span class="day">10</span> <span class="year">2011</span> </div>
           </div>
        <div class="article-preview">
            <h1><a href="/news/newsroom/news-and-numbers/032012.html">Six in 10 Obese Adults Have Joint Pain</a></h1>
            <!--?psx-end-slot ahrqTranslation?-->
          </div>

      </article>

      <article class="articles">
        <div class="article-preview-left-col">
          <div class="article-preview-date"> <span class="month">January</span> <span class="day">31</span> <span class="year">2010</span> </div>
        </div>
         <div class="article-preview">
            <h1><a href="/news/newsroom/news-and-numbers/012512.html">Hospital Stays Involving C. difficile Infections Leveled Off After 300 Percent Increase Since 1993</a></h1>
            <!--?psx-end-slot ahrqTranslation?-->
          </div>

      </article>

      <article class="articles"> 
        <div class="article-preview-left-col">
          <div class="article-preview-date"> <span class="month">January</span> <span class="day">30</span> <span class="year">2012</span> </div>
        </div>
         <div class="article-preview">
            <h1><a href="/news/newsroom/news-and-numbers/012512.html">Hospital Stays Involving C. difficile Infections Leveled Off After 300 Percent Increase Since 1993</a></h1>
            <!--?psx-end-slot ahrqTranslation?-->
          </div>

      </article>

      <article class="articles">
        <div class="article-preview-left-col">
          <div class="article-preview-date"> <span class="month">January</span> <span class="day">15</span> <span class="year">2012</span> </div>
        </div>
         <div class="article-preview">
            <h1><a href="/news/newsroom/news-and-numbers/012512.html">Hospital Stays Involving C. difficile Infections Leveled Off After 300 Percent Increase Since 1993</a></h1>
            <!--?psx-end-slot ahrqTranslation?-->
          </div>

      </article>

      <article class="articles">
        <div class="article-preview-left-col">
          <div class="article-preview-date"> <span class="month">January</span> <span class="day">27</span> <span class="year">2012</span> </div>
        </div>
        <div class="article-preview">
            <h1><a href="/news/newsroom/news-and-numbers/011212.html">Most Health Care Costs Incurred by Few Americans</a></h1>
            <!--?psx-end-slot ahrqTranslation?-->
          </div>

      </article>

      <article class="articles">
        <div class="article-preview-left-col">
          <div class="article-preview-date"> <span class="month">January</span> <span class="day">25</span> <span class="year">2012</span> </div>
        </div>
        <div class="article-preview">
            <h1><a href="/news/newsroom/news-and-numbers/030712.html">Hospital Readmission Rates Higher for Chronic Conditions</a></h1>
            <!--?psx-end-slot ahrqTranslation?-->
          </div>

      </article>
`
$(文档).ready(函数(){
函数sortUsingNestedText(父、子选择器、键选择器、调用者){
var条款;
var items=parent.childSelector.sort(函数a,b){
var vA=$(keySelector,a).text();
var vB=$(keySelector,b).text();
返回值(vAvB)?1:0;
});
var文章=$();
项。每个(函数(){
if($(this).text().indexOf(“articles”)!=-1){//如果它包含“articles”
articles=articles.add($(this));//添加到articles jQuery对象
items=items.not($(this));//并从items中删除
}
});
if(caller.data('order_by')=='asc'){
articles=articles.sort(函数(a,b){
var vA=$(a).text();
var vB=$(b).text();
返回值(vAvB)?1:0;
});
}
if(caller.data('order_by')=='desc'){//alert('desc pattern');
caller.data('order_by','asc');
parent.append(items.get().reverse());
parent.append(articles.get().reverse());
}else{//alert('asc模式');
caller.data('order_by','desc');
母公司。附加(条款);
父项。附加(项);
}
}
$(“#date”).data(“sortKey”、“div.article-preview-date”).data(“order#u by”、“desc”);
$('#name')。数据(“排序键”,“div.article-preview”)。数据('order#u by','desc');
$(“#日期”)。单击(函数(){
$('.article preview left col>.article preview date>span')。contents().unwrap();
$('.blog year month').remove();
sortUsingNestedText($('#sort'),“article”,$(“#date”).data(“sortKey”),$(this));
});
$(“#名称”)。单击(函数(){
$('.article preview left col>.article preview date>span')。contents().unwrap();
$('.blog year month').remove();
sortUsingNestedText($('#sort'),“article”,$(“#name”).data(“sortKey”),$(this));
});
}) 
2012

`

那么这个问题是关于排序还是字符串转换的呢?你的标题和问题应该一致。