首页 \ 问答 \ 如何使用NSSortDescriptor对NSMutableArray进行排序?(How to sort a NSMutableArray using NSSortDescriptor?)

如何使用NSSortDescriptor对NSMutableArray进行排序?(How to sort a NSMutableArray using NSSortDescriptor?)

我正在从JSON webservice解析数据,然后使用以下代码按价格,日期,折扣等对数据进行排序。

这是我用来对数据进行排序的代码:

-(void)priceSort:(id)sender {

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc]
                                     initWithKey: @"price" ascending: YES];

NSMutableArray *sortedArray = (NSMutableArray *)[self.displayItems
                                          sortedArrayUsingDescriptors: [NSArray arrayWithObject:sortDescriptor]];

[self setDisplayItems:sortedArray];

[self.tableView reloadData];


}

当我尝试按价格排序时,这种方法很好,但是当我想按照评论数排序时,我似乎无法正确地获取代码:

我用的价格:

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc]
                                         initWithKey: @"price" ascending: YES];

但对于评论,我想得到“n”值。 看看它是如何嵌套在输出中的(显示项目mutableArray如下:

"old_price" = 24;
        price = "9.9";
        reviews =         {
            **n = 11;**
            val = 70;
        };
        "sold_count" = 101;

谢谢你对此有任何帮助:)


I'm parsing data from JSON webservice and then using the following code to sort the data by price, date, discount etc.

here's the code I'm using to sort the data:

-(void)priceSort:(id)sender {

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc]
                                     initWithKey: @"price" ascending: YES];

NSMutableArray *sortedArray = (NSMutableArray *)[self.displayItems
                                          sortedArrayUsingDescriptors: [NSArray arrayWithObject:sortDescriptor]];

[self setDisplayItems:sortedArray];

[self.tableView reloadData];


}

this works fine when I'm trying to sort by price, however when I want to sort by number of reviews I can't seem to get the code right:

for price I use:

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc]
                                         initWithKey: @"price" ascending: YES];

but for reviews I want to get the "n" value. see how it's nested in the output (of the display Items mutableArray below:

"old_price" = 24;
        price = "9.9";
        reviews =         {
            **n = 11;**
            val = 70;
        };
        "sold_count" = 101;

thanks for any help on this :)


原文:https://stackoverflow.com/questions/15027377
更新时间:2023-12-06 17:12

最满意答案

我终于制作了自己的jQuery插件。 它管理页面交换和文件的ajax加载,在显示每个页面之前和之后触发自定义事件。 收到的评论不超过150行。 不过,我保留了参数传递的东西。


I finally made my own jQuery plugin. It manages the page-swapping and the ajax loading from files, firing custom events before and after each page is shown. Took no more than 150 lines comments included. I kept the parameter-passing stuff out, though.

相关问答

更多
  • 我认为这是大多数网站在考虑用户今天使用的设备数量以及各自的能力和限制时应该考虑的方向。 重要: 在阅读其余部分之前,请理解这个概念是建立在网络设计基本原理的基础上的。 为了为所有设备和情况设计单页应用程序,它不能作为单页应用程序专门运行。 您必须建立一个基础,可以在基本功能非常有限的浏览器上工作,并根据其设备的功能增强用户体验。 这可能会给你带来更多的工作,但是你将能够满足更大和更多样化的观众,这比将专门为现代桌面或手机浏览器构建的网络应用程序集中在一起更令人印象深刻。 减少加载时间和/或重量 单页应用程序 ...
  • 看到这个答案 。 就开发工作而言,过度简化的比较是 多页面应用程序: HTTP请求中包含的用户操作(例如HTTP POST,HTTP GET) 请求被处理,服务器响应HTTP页面 缺点:需要很多页面才能响应每个用户的操作(例如,将项目添加到列表,更新表单数据),而在SPA数据绑定中可以为您更新视图。 其次,Web应用程序负责维护整个页面请求之间的状态。 相反,当浏览器停留在同一页面上时,SPA可以在XHR之间保持其状态,而无需额外的工作。 单页应用程序(SPA): 在客户端和服务器端配置路由 代码“控制器” ...
  • SPA(单页面应用程序)与使用Django或Backbone无关。 您可以在使用Django时编写SPA。 但你也可以使用Backbone和Django。 有关更多信息,请阅读以下帖子: http://sidazad.tumblr.com/post/52201957429/web-2-0-the-switch-to-client-javascript-frameworks http://sidazad.tumblr.com/post/52591790157/the-search-for-the-perfec ...
  • 我建议按功能组织您的应用程序,其中每个功能都是一个Angular模块。 看到这里的例子https://github.com/angular-app/angular-app : + src + admin + users . admin-users-add.js . admin-users-add.tpl.html . admin-users-edit.js . admin-users.edit.tpl.html . admin.js + ...
  • 如果您选择使用单页概念设计应用程序 - 那么大多数css / js文件只会加载,其他逻辑将使用javascript(模板,调用后端等)完成。单页方法也需要有特定的后端 - 在大多数情况下,存在具有xml,json端点的RESTful服务。例如,当您有单个页面 - 并且您想要获取有关某些(例如)产品的信息时,您需要使用url调用后端 - HTTP:// {后端主机} / API /产品/ 1 和后端应该返回(示例): { "id": 1, "name" : "product1" } 您将使用 ...
  • http://docs.angularjs.org/已经完成了你在这里描述的大部分内容。 Disqus对我们来说效果很好。 随意查看源代码。 一切都在github上。 http://docs.angularjs.org/ already do most of what you described here. Disqus works quite well for us. Feel free to look at the source code. Everything is on github.
  • 我刚刚在链接中测试了解决方案,并为摘要添加了一些更新: 转到母版页库以下载您需要自定义的母版页:SharePointSite / _catalogs / masterpage,例如minimal.master和mininal.html 在visual studio中创建新的空sharepoint项目,添加新模块,添加您下载的主页 自定义模块中的Element.xml,并确保以下链接类似:Module Name =“MasterPages”List =“116”Url =“_ catalogs / maste ...
  • 我终于制作了自己的jQuery插件。 它管理页面交换和文件的ajax加载,在显示每个页面之前和之后触发自定义事件。 收到的评论不超过150行。 不过,我保留了参数传递的东西。 I finally made my own jQuery plugin. It manages the page-swapping and the ajax loading from files, firing custom events before and after each page is shown. Took no mor ...
  • 扩展库版本附带了几个updateSite拉链: updateSiteOpenNTF.zip updateSiteOpenNTF-designer.zip updateSiteOpenTNTFExtended.zip updateSiteOpenNTFSamples.zip 确保使用的是updateSiteOpenNTF-designer.zip ,而不是标准的updateSiteOpenNTF.zip。 顾名思义,设计器zip包含专门为Designer设计的extlib内容,例如Single App Wi ...
  • 是的, Gmail是一个单页应用程序。 这就是加载Gmail之前你看到进度条的原因,因为它从服务器获取所有内容,然后在客户端完成所有渲染,只调用服务器获取数据而不是DOM。 除了Gmail之外,Google Calendar,Google Drive等套件中的其他服务也是单页应用程序。 Yes, Gmail is a single-page application. That is the reason before loading Gmail you see a progress bar, as it f ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)