Javascript 如何排列升序和降序?
我是AngularJS的新手。我的问题是我想根据用户输入对数组进行排序。例如,如果用户输入-rating,它应该按照更高的评级顺序进行排序,如果用户输入-date,它应该按照最新的顺序进行排序。我知道我应该使用OrderBy筛选器,但我无法构建机制。谢谢你的帮助 下面是js代码Javascript 如何排列升序和降序?,javascript,angularjs,Javascript,Angularjs,我是AngularJS的新手。我的问题是我想根据用户输入对数组进行排序。例如,如果用户输入-rating,它应该按照更高的评级顺序进行排序,如果用户输入-date,它应该按照最新的顺序进行排序。我知道我应该使用OrderBy筛选器,但我无法构建机制。谢谢你的帮助 下面是js代码 <script> var app = angular.module('confusionApp',[]); app.controller('dishDetailController', function()
<script>
var app = angular.module('confusionApp',[]);
app.controller('dishDetailController', function() {
var dish={
name:'Uthapizza',
image: 'images/uthapizza.png',
category: 'mains',
label:'Hot',
price:'4.99',
description:'A unique combination of Indian Uthappam (pancake) and Italian pizza, topped with Cerignola olives, ripe vine cherry tomatoes, Vidalia onion, Guntur chillies and Buffalo Paneer.',
comments: [
{
rating:5,
comment:"Imagine all the eatables, living in conFusion!",
author:"John Lemon",
date:"2012-10-16T17:57:28.556094Z"
},
{
rating:4,
comment:"Sends anyone to heaven, I wish I could get my mother-in-law to eat it!",
author:"Paul McVites",
date:"2014-09-05T17:57:28.556094Z"
},
{
rating:3,
comment:"Eat it, just eat it!",
author:"Michael Jaikishan",
date:"2015-02-13T17:57:28.556094Z"
}
]
};
this.dish = dish;
var-app=angular.module('confusionApp',[]);
app.controller('dishdailcontroller',function(){
真空皿={
名称:'Uthapizza',
图片:“images/uthapizza.png”,
类别:'干线',
标签:'热',
价格:'4.99',
描述:“一种独特的印度乌萨帕姆(煎饼)和意大利比萨饼组合,顶部有樱桃橄榄、成熟的葡萄藤樱桃西红柿、维达利亚洋葱、甘特辣椒和水牛肉饼。”,
评论:[
{
评级:5,
评论:“想象一下所有的食物,生活在混乱中!”,
作者:“约翰·莱蒙”,
日期:“2012-10-16T17:57:28.556094Z”
},
{
评级:4,
评论:“把任何人送到天堂,我希望我能让我岳母吃!”,
作者:“Paul McVites”,
日期:“2014-09-05T17:57:28.556094Z”
},
{
评级:3,
评论:“吃吧,就吃吧!”,
作者:“迈克尔·贾基尚”,
日期:“2015-02-13T17:57:28.556094Z”
}
]
};
这道菜=这道菜;
下面是html代码
<div class="col-xs-9 col-xs-offset-1">
<div class="row">
<div class="col-xs-6">
<div>
<h4>Customer Comments</h4>
</div>
</div>
<div class="col-xs-6">
<div>
<h5>Sort by: <input type="text"></h5>
</div>
</div>
</div>
<ul class="media-list">
<li class="media" ng-repeat="comm in dishCtrl.dish.comments ">
<blockquote>
<p>{{comm.rating}}</p>
<p>{{comm.comment}}</p>
<footer>{{comm.author}} ,<cite>{{comm.date}}</cite></footer>
</blockquote>
</li>
</ul>
</div>
客户意见
排序方式:
-
{{comm.rating}}
{{comm.comment}}
{{comm.author},{{comm.date}
将ng模型添加到文本输入中,并在orderBy筛选器中使用此模型:
<div class="col-xs-9 col-xs-offset-1">
<div class="row">
<div class="col-xs-6">
<div>
<h4>Customer Comments</h4>
</div>
</div>
<div class="col-xs-6">
<div>
<h5>Sort by: <input type="text" ng-model="sortorder"></h5>
</div>
</div>
</div>
<ul class="media-list">
<li class="media" ng-repeat="comm in dishCtrl.dish.comments | orderBy:sortorder">
<blockquote>
<p>{{comm.rating}}</p>
<p>{{comm.comment}}</p>
<footer>{{comm.author}} ,<cite>{{comm.date}}</cite></footer>
</blockquote>
</li>
</ul>
</div>
客户意见
排序方式:
-
{{comm.rating}}
{{comm.comment}}
{{comm.author},{{comm.date}
始终保持简单思考。我理解thnx,感谢您的帮助。如果您或其他在该领域有经验的人能够实现方法,这对其他人来说可能会很好,他们将再次查看此页thnx。您的回答解决了我的问题:)