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 1114523
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T03:01:07+00:00 2026-05-17T03:01:07+00:00

in c# if i have an arraylist populated like (ID, ITEMQUANTITY), and i would

  • 0

in c# if i have an arraylist populated like (ID, ITEMQUANTITY), and i would like to compare them by ID, how would i do that?
I mean, i need to customize it so that i can compare it by the first value only, so if i want to insert another item i can check if the id is already in the list….
I know how to do it by looping through all the items in the arraylist, but I remember doing this in Java a while ago with overriding some interface or methods or something…

Currently i’m using SortedList which I can compare by KEY of (KEY, VALUE).
But, the problem is, i dont want the items to be sorted…
I want it to be so the last one in gets put on the last place.
Maybe i can bypass converting to arraylist if i could just set the SortedList not to sort items…

Tnx!

Andrej

  • 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-17T03:01:08+00:00Added an answer on May 17, 2026 at 3:01 am

    A few approaches, with different pros and cons:

    list.Any(item => item.ID == newItem.ID) returns true if there’s a matching ID, though this is the same as loop (and indeed slightly more expensive because of the lambda) but cleaner code.

    Maintaining a HashSet where the comparator compares on the ID property would mean that new values with an existing ID would not be added.

    Maintaining the list in ID order would allow you to quickly either find an existing object in O(log n) time complexity by using BinarySearch(), or else find the location you should insert this new item to maintain the order.

    If ID truly identifies the objects (that is to say, when the IDs are the equal the objects should be considered equal) then implementing IEquatable<T> to compare based on ID, overriding object.Equals() to call into that type-specific equality method, and overriding GetHashCode() to return the value of ID (if it is int, or a smaller integeral type, or cast to int if it’s uint) or ID’s hashcode (if it’s a different type) will mean that this becomes the default concept of identity, meaning that HashSet won’t need a special comparator, and Contains will do the work for you (note that Contains is essentially a loop too).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an ArrayList<String> that I'd like to return a copy of. ArrayList has
I have an arraylist that contains items called Room. Each Room has a roomtype
I need to make an ArrayList of ArrayLists thread safe. I also cannot have
In Java, say you have a class that wraps an ArrayList (or any collection)
I have a heavily populated arraylist, which I want to clear and reuse. If
I have a listview that's populated by rows that get their data from a
I have set up a HashMap like so: Map<String, ArrayList<String>> theAccused = new HashMap<String,
I have an ArrayList of objects that have a name and an icon pointer
Can't figure this one out I have an ArrayList of classes: // Holds an
i have created synchronized arrayList like this import java.text.SimpleDateFormat; import java.util.*; class HelloThread {

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.