Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7501963
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T20:42:30+00:00 2026-05-29T20:42:30+00:00

My app contain two array (ie productArray and priceArray) and pass the value of

  • 0

My app contain two array (ie productArray and priceArray) and pass the value of one array on cell. textlabel.text and other array on cell.detaillabel.text.It is working fine .Now i want to sort uitableview in ascending price list. please guide me how to do it.. Here Is my code..

 #import <UIKit/UIKit.h>

@interface RootViewController : UITableViewController {
NSMutableArray *productArray;
NSMutableArray *priceArray;

}

@property(nonatomic,retain)NSMutableArray *productArray;
@property(nonatomic,retain)NSMutableArray *priceArray;
@end



 #import "RootViewController.h"



 @implementation RootViewController
 @synthesize productArray,priceArray;


 - (void)viewDidLoad {
   [super viewDidLoad];

    productArray=[[NSMutableArray alloc]initWithObjects:@"Apple",@"iphone",@"ipod",@"ipad",nil];
priceArray=[[NSMutableArray alloc]initWithObjects:@"4000",@"2000",@"100",@"1000",nil];

// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
 }


 #pragma mark -
 #pragma mark Table view data source

  // Customize the number of sections in the table view.
 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  return 1;
}


 // Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section       {
  return [productsArray count];
}


  // Customize the appearance of table view cells.
  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:     (NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}

// Configure the cell.
cell.textLabel.text=[productsArray objectAtIndex:indexPath.row];
cell.detailTextLabel.text=[priceArray objectAtIndex:indexPath.row];

return cell;
}

 - (void)didReceiveMemoryWarning {
  // Releases the view if it doesn't have a superview.
  [super didReceiveMemoryWarning];

// Relinquish ownership any cached data, images, etc that aren't in use.
 }

- (void)viewDidUnload {
// Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
// For example: self.myOutlet = nil;
}


- (void)dealloc {
  [super dealloc];
 }


 @end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T20:42:32+00:00Added an answer on May 29, 2026 at 8:42 pm

    Reading values from two different arrays to populate a tableview cell makes sorting difficult. I recommend you make a single object that contains both the product and price attributes, then store these objects in a single array. Then you can simply sort the array with a custom sorting method that compares the prices.

    Example product class:

    @interface Product : NSObject
    {
        NSString *productName;
        NSString *price;
    }
    @property (nonatomic, retain) NSString *productName;
    @property (nonatomic, retain) NSString *price;
    

    Then write a method that can sort products by price:

    products = [products sortedArrayUsingSelector(sortByPrice:)];
    

    …which calls this method in your products implementation:

    - (NSComparisonResult)sortByPrice:(id)anObject
    {
        if (self.price < anObject.price) {
            return NSOrderedAscending;
        } else (if self.price > anObject.price) {
            return NSOrderedDescending;
        }
        return NSOrderedSame;
    }
    

    Store products in one array:

    NSMutableArray *products;
    

    And your cell values will be set by an object from a single, sorted array.

    Creating a product changes from this:

    [products addObject:@"Milk"];
    [prices addObject:@"2.25"];
    

    to this:

    Product *newProduct = [[[Product alloc] init] autorelease];
    [products addObject:newProduct];
    [newProduct setProductName:@"Milk"];
    [newProduct setPrice:@"2.25"];
    

    And setting your cell values changes from this:

    cell.textLabel.text=[productArray objectAtIndex:indexPath.row];
    cell.detailTextLabel.text=[priceArray objectAtIndex:indexPath.row];
    

    to this:

    cell.textLabel.text=(Product *)[products objectAtIndex:indexPath.row].productName;
    cell.detailTextLabel.text=(Product *)[products objectAtIndex:indexPath.row].price;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on a navigation-based iPhone-only app that serves two main purposes: One, to
I've set up my app's user defaults to contain two toggle switches - one
I have two targets for my project: one is the App, and the other
I created two files in the App_GlobalResources folder: SiteResources.en-US.resx SiteResources.sp-SP.resx Both contain a value
I'm working on a Rails app that will contain information on a bunch of
My app receives numerous text strings which may or may not contain a URL
I have to validate username in my app so that it cannot contain two
I have the following two routes in my app: Router::connect('/posts/:id', array('controller' => 'posts', 'action'
I have an MFC sdi app that uses a splitter window to contain a
I'm writing an app for gnome which will support plugins. Each plugin will contain

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.