Best way of sorting a Vector by member variable

I have a Vector of objects called Node. Each Node has various member variables, one of which is an integer called cost.
What would be the best way of sorting this vector in ascending order of cost?
I tried Bubble Sort but once the Vector starts getting big, it is SO slow.
Any help much appreciated.
Thanks,
Peter

Declare your Node like this:public class Node ... implements Comparable {
  // your other code here
  public int compareTo(Object o) {
    Node otherNode = (Node)o;
    return this.cost - otherNode.cost;
}That code provides a "natural ordering" for your Node class. Then to sort your Vector, just do this:Collections.sort(yourVector);I don't know what kind of sort that will do, but no doubt it will be better than your bubble sort.

Similar Messages

  • What's the best way to sort query results?

    Hello All,
    I have a standard CRUD UI that now needs the results sorted, based on input from the UI. What's the best way to sort results by entity fields?
    I'm familiar with the conventional Java methodology using a TreeSet and comparator. Is this the best route, does BDB JE offer more convenient or performant alternatives?
    I looked through the documentation and saw how to change the indexes, but I'm just looking to change the order in the scope of the query.
    If my application were an address book, the UI would be sortable, ascending and descending, by date added, first name, last name, etc. based on which column the user clicked on.
    Thanks in advance,
    Steven
    Harvard Children's Hospital Informatics Program

    Hi Steven,
    Using standard Java collections is probably the best approach.
    One thing that may be useful is to get a SortedMap (Primary or SecondaryIndex.sortedMap) for the case where you have an index sorted the way you want, and then copy the primary index into a TreeMap (with a comparator) for other cases. That way, you always have a SortedMap to work with, whether you're copying the index or not.
    I haven't tried this myself, just a thought.
    --mark                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

  • Best way to get the values of local variables

    What is currently the best way to get the values of local variables. I know it is not currently possible to set watchpoints on local variables, but what are the best workarounds?

    You have to use StackFrame methods, eg, visibleVariables and getValues. To get a StackFrame for a thread, that thread has to be suspended, eg, by a BreakpointEvent.

  • Best way to sort data from a file alphabetically

    If I have a file as such:
    FIRST_NAME|LAST_NAME
    I want to display the data in alphabetical order by last name and by first name. what is the best way to do this? I found java.util.Arrays
    but i thought that it might be best if i read the data into a linked list. I want to display it on a form, so that I can press a next and previous button. Next will show the next record in alphabetical order after the previous one(B->C) and previous would go (C->B)
    thanks.

    How about this:
    Create a PersonRecord object, and have it implement Comparable.
    import java.util.Comparator;
    public class PersonRecord implements Comparable < PersonRecord >
        public static Comparator < PersonRecord > surnameComparator ()
            public int compare (PersonRecord pr1, PersonRecord pr2)
                return pr1.getSurname ().compareTo (pr2.getSurname ());
        public static Comparator < PersonRecord > nameComparator ()
            public int compare (PersonRecord pr1, PersonRecord pr2)
                return pr1.getName ().compareTo (pr2.getName ());
    }See java.util.Comparator, java.lang.Comparable.
    Then use Arrays.sort(List, Comparable) at your own discretion...
    I've said enough; work that part out yourself.
    lutha

  • What is the best way to sort an XML document in WLI 8.1?

    I have the schema for the XML and I want to sort the data. What is the best way to do this?

    I'm not sure if you want the result of the sorting to be a sorted XML document or a sorted object tree but anyway both XSLT and XQuery support sorting so you could use a Transformation control to execute an XQuery or XSLT transformation to sort the data. Another option (if you are not comfortable writing XQuery or XSLT) would be to use XmlBeans and programmatically sort the data. A word of warning though, these processes are quite memory intensive and if the XML document you are sorting is large (say over 10 MB) your server might run out of memory. In this case you might be better of just parsing the document with SAX and then sorting the result objects.

  • What is the best way for sorting?

    Hi i have 1000 customers in the database. I need to display them in the combo box. combox is editable. As the user types the letters i need to filter and display the customer names. for example if the user types 'a' in the combox i have to display all the customer names that starts with 'a'. if the customer types 'as' then i have to display only names that start with 'as' in the combo box popup. for every key stroke i have to filter and display the customer names.
    for this there are two approaches:-
    1. for every key stroke go to database and retrieve the sorted results and display them. In this way sorting happens in the database.
    2. pull all the customers into java once, cash them and sort them accordingly based on the key strokes. In this sorting happens in our java code.
    few friends said that going to databse for every key stroke is expensive. I feel pulling all the customers at once and cashing them in java is not a good idea.
    I am really confused. I need an expert suggestion. which way is best? why is it the best way? what are the pros and cons in both the ways. please help me.
    Thanks in advance.

    Thanks ejp but initially if the combo box is not empty. that means if he did not typed any letter i will be displaying all the names.No. That's the mistake. Don't do that. A combobox with 1000 or 5000 items is completely useless.Do it the way I said.
    My problem is if there are 5000 customers then what? will it not create a performance issue.Of course it will. It will also create a major performance issue for the user.
    However you seem to have missed something else. By populating the combobox with anything, you are already running a cache. No two ways about it, and no way to avoid it. So whatever you populate it with, and whenever you do it, all you have to do when the user types another keystroke is filter the combobox model to exclude everything that doesn't match the prefix he has typed. The issue of going back to the database doesn't even arise.

  • Best way to sort (short of dragging each one)

    With so many apps, sorthing them into a working order one-by-one is a bear, especially across multiple pages - many times the app stops on a page and I can't get it to go to the next page, then apps get displaced and it causes changes on another page and so on and so on...
    Is there a better way to sort, maybe a master list of some sort, that I can access to set them in the order I want them to appear?

    No such option exists - at the present time anyway.
    You can use this link to provide Apple your feedback regarding this.
    http://www.apple.com/feedback/iphone.html

  • What is the best way to retrive data from a Global Variable?

    Here is what I want to do,
    I have several PC's that run different types of tests. I want to use a global variable, running on a single PC, that acts like a sever that can be accessed by the other PC's in my lab. This Global variable will store the hostnames of the different PC's that are currently running each test, along with a description of the test.  Then, a user can access this Global variable to read the different values and select the PC and connect to it's desktop using Remote Desktop in Windows.
    Is it possible to write data to the Global variable that is running on the single PC?
    What is the best way to do this? Does anyone have a sample VI?
    What is the best way to then read the data from the Global variable?
    (I will probably use an array\cluster to store the hostnames.) 

    Another pre-LV8 idea...
    A functional global can be accessed using VI-Server and called using "call by reference".
    This approach harnesses the TCP functionality built into the VI-Server to manage the conncetion.
    This can be pretty quick and (if the functional global is written correctly) will support buffered- mixed data types. (Try to do that with the Shared Variable  ).
    Just another idea,
    Ben
    Ben Rayner
    I am currently active on.. MainStream Preppers
    Rayner's Ridge is under construction

  • Best Way To transfer screen elements to ABAP variables

    Hi all,
    whats the best way to attach the abap elements to screen elements
    normally earlier versions before 4.7C i always used TABLES : MARC in abap program
    and in screen design i used to click "GET from DICTIONARY" to design the screen in Screen painter
    The advantage with this approach is all data base elements are attached to our screens like search helps
    atomatic checks etc and more over all the information valid for a particular plant are retrived by the system search helps themselves like
    for example on the screen we have screen elements/ fields  Plant , MRP controller.
    in the plant i typed  "X110'.
    now in the mrp controller i pressed F4 to search for valid mrp controller . Since we associated the screen elements with database elements with TABLES statement all the MRP controller relevent to that plant X110 are displayed. this is quite valid.
    But i read usage of tables : in Object Oriented abap is Obsolete is this still the correct way of programming.
    I can also use Some thing like this Data : st_marc type type MARC.
    and in Screen design i can click "Get from program" This approach will loose all the database assignments to screen elements.
    Now i can no longer retrieve the MRP controller relevent for plant 'X110'. F4 will show me all the MRP controllers in all plants.
    Is there any other approach to do this. Thanks.
    Edited by: Kavitha on Mar 13, 2008 2:37 PM

    Please read the following two links. These experts explain it better than I could put in words!:
    /people/thomas.jung3/blog/2005/09/08/oo-abap-dynpro-programming
    /people/horst.keller/blog/2007/04/24/from-abap-ipo-to-abap-objects-and-the-separation-of-concerns
    https://www.sdn.sap.com/irj/sdn/go/portal/prtroot/docs/library/uuid/4fbafc9e-0e01-0010-dea9-9d23d1b269fb  (Under section Modularizing your programs)

  • Best way to sort a Table

    Hi Gurus,
    I'm using a EJB to read Data from a DB and write it into a Context Node "SourceNode" in my Component Controller.
    In the View, I've got a Node "DisplayNode", which is not mapped to "SourceNode" and but is filled using a supplaymethod that uses WDCopyService to copy the content of "SourceNode" to "DisplayNode". In the View, I've got a Node "FilterNode" that holds the entered filter attributes and a Node "TempNode" for looping on the diffrent filter attributes.
    So everytime filter attributes are entered, I reduce the content of "DisplayNode" and when the filter attributes are reseted, I copy back the hole "SourceNode" to "DisplayNode".
    The question is, if there is a more efficient way to do this, since if my SourceNode hast got 10.000 an more entries, this gonna take long to filter and to reset filter.
    Thanks in advance.
    Helpful answers will be rewarded with points.

    This is semi-related to my other post about the MINUS statement. The full query that I'm playing with can be viewed there.
    Currently, two field of the table must be specified in the where clause of the query: a bank_account_id and a date range. There are 4 other fields that can be used to restrict the search results, but they are not expected to be a primary means of searching.
    The anticipated number of records in the table at any given time is expected to be between 1 million and 1.5 million records spanning 50 bank accounts over a period of 3-6 months.
    Currently the indexes that I have specify the bank account ID as the first field of every index followed by the fields in the order by clause (which include the date field).
    -Shawn Smiley
    null

  • What is the best way to sort and separate tv show episodes

    Hi Everyone,
    I have some tv shows converted for my apple tv but they go over a period of several  seasons, how can I merge and separate them by season year  and  have them in itunes as each season  with  only those shows for that season ..  any tips and hints   all help is appreciated thanks

    Use -> MetaZ to tag the files by title, season and episode (and other info).

  • Best way to deal with photos from the start? (Bridge CS5 and iPhoto)

    Hi all,
    I'm just learning the ropes of the whole CS5 suite, and was starting to add keywords etc. in Bridge when I realized I couldn't access the files in my iPhoto ('09) library. I've read a few threads about this issue, but haven't really found a consensus on how to deal with it.
    I figure I'm in a good position to get things right from the start since I have no tags or ratings etc. in my iPhoto setup (and have only added keywords to the few pics that weren't in my iPhoto Library), and am wondering what you think is the best way to sort photos given I plan on using them in web design etc., and all the parts of my CS5 suite.
    If getting the photos out of iPhoto is best what's the best way to do that? I've read suggestions to right click on the library and click on "Show Contents"- or something like that, but that does not come up as an option when I right click my iPhoto Library in Bridge. Thanks so much for your help!
    Please let me know if there is any info I could give you that would be helpful in order to figure this one out.
    Best,
    Mara

    I must be far too lazy to be sure.  In iPhoto if I want to move an image all I
    do is click and drag from iPhoto to the desktop.
    Here comes the trouble with using iPhoto and mixing applications. You have
    to be sure what you do in iPhoto because you easily grab the wrong version
    instead of the original.
    As Tai Lao pointed out the the difference between Bridge and iPhoto in short
    is open visible structure for Bridge and hidden invisible library for
    iPhoto.
    There may be perfect and valid reason to use iPhoto but mixing the two
    workflows without thinking or knowing what you do is bound to get you sooner
    or later in trouble with wrong versions of the file.
    And clicking and dragging from iPhoto is easy to mistake in those versions.
    1 - I used to create incredible folder/sub-folder arrangements depending upon
    operating system file management to find files.
    You can create whatever structure you want including a thought through
    naming convention that suites your needs, if you keep doing this in Bridge
    you can easily open and close folders using the folder panel, in iPhoto you
    have a lot of events and albums but none as clear as in Bridge. (at least
    that is my opinion...)
    Also I would prefer to use a good naming convention with less folders and
    subfolders instead of bad file naming and many well named folders. Also
    using the metadata options description and keywords let's you easily find
    the files you want but when having the need to retrieve the original from an
    external disk or DVD using less folders will be quicker to find.
    Since depending upon metadata keywords or star ratings I now prefer to have
    all images (originals and amended) in one single folder as Bridge (or iPhoto
    for that matter) can only see the images in the folder it is looking at.
    Wrong, using the find option (cmd+F) you can specify Bridge to look in main
    folders or entire disk, include the option to look in subfolders and you are
    in business (but the first time Bridge caches it also need to index all the
    metadata, that takes some time but only the first time.
    2 - potential feature request for Bridge?
    To have an option where Bridge can see and display images in subfolder
    arrangements from the folder it is looking at.
    That is already there since a view versions
    Select a folder with subfolders and then from the menu view select 'show
    items for subfolders'
    It is still not a perfect way of functioning but it gets better every new
    version, in CS5 the speed of gathering the files is very much improved.
    However here is iPhoto the winner because it keeps the files in cache (or
    something like that) Bridge is not the best in this behavior but it is
    possible to use.
    This is a bit like a restriction of OS X "All images" to all images in the
    selected folder and all subfolders of the selected folder (if that makes any
    sense at all)
    Many users have tons of files on there system, why would you use that option
    to view all if you are perfectly capable of finding the files or file types
    you want in most applications (but you can also find them in OSX, you want
    psd files do a search for ".psd" and you will see all the psd files on your
    system.
    3 - iPhoto can't really handle large files.  With one event consisting of
    4,500+ images weighing in at 884 MB it is zippy.  Were those to be originals
    at a high resolution iPhoto would probably be tediously slow and require a
    folder/sub-folder arrangement which, to me, seems to undermine the importance
    of managing stuff using keywords.
    IPhoto is not designed to be a professional application, therefore you can't
    expect behavior for this
    In Bridge you have no limit of files on your system only the limit for the
    cache is somewhere around 500.000 I believe. But even Bridge is not really
    designed for handling that amount of files in a pleasant way.
    I also don't know of an application that can do so for normal retail prices,
    most users of that amount of files have custom made or customized
    application that costs many thousands dollars. Bridge is in fact a free gift
    when buying a Suite application from Adobe.
    The file size for viewing in Bridge is by default set to a max of 1 GB but
    that is for building previews you can see thumbs and small previews of
    bigger files. In the Bridge preferences you can set the limit for this to
    whatever you want, it only grows the cache file for previews when using many
    large files.
    In summary: Bridge is great for CS5 partnering on metadata, original images or
    large filesizes. 
    I couldn't agree more!
    iPhoto is great on sharing images across devices and better
    still if those files are low res with small filesizes?
    I couldn't disagree more...
    You can do all of this using Bridge to start own created actions or with
    scripts like image processor.
    Do some research on Adobe TV or view other video tutorials on the web, there
    are many free and good tutorials. You will learn that you can do all you
    want in Bridge (and a lot more...)

  • What is the best way to scan and sort old photos in iPhoto

    What is the best way to scan and sort old photos in iPhoto?  They do not have digital dates.

    Hey Chicago Sue,
    Once you scan them and have them on your desktop. You should use Automator and assign the common IPTC tags to the images, so that when you do import them into iPhoto, they get recorded.
    Here is an example of an action in Automator:

  • Best way to import vector files?

    What is the best way to import .AI and .EPS files? Is File > Import the way to go or is there another way in CS5?
    Thanks.

    If you want to place the vector file within an existing file then import is the way to go, if you want to open it in a new window then open will do that. In either instance you will still get the Vector File Options dialog box that allows you to make some modifications while importing/opening the file.

  • Best way to make a clipping mask with indesign drawn vector shapes?

    What is the best way to create a clipping mask  with vector art that is drawn inside of indesign?
    I have a group of vector shapes to paste into a rounded corner box. This art is grouped.
    What i tried:
    -create the rounded corner box i want the art inside
    -cut art
    -edit > paste-into
    This worked, but i could not figure out how to move the artwork once it was pasted into the shape. With the direct select tool i could move the individual objects, but not the group of objects. Any ideas?
    Is this the best way to acomplish what i'm trying to do?
    Thanks!

    Yopu don't mention the version of ID, which makes a difference here.
    Paste Into is correct. In CS5 you can then use the content grabber donut to move the group inside the frame. In all versions you should be able to select the frame, tehn use Object > Select > Content to get the group, or use the button for that onthe Control Panel. Before CS5 you'll need to use the arrow keys or grab the center spot withthe mouse to move the group.

Maybe you are looking for

  • Wont let me post pictures on Facebook and overheating!!

    I have had this phone for a few weeks now and I love it however a few issues have started creeping up, Firstly I wanted to post some pictures on facebook, I attached them to a post but when I clicked 'post' it just totally came out of the app so I th

  • I dropped my iPhone 4s down the toilet and now the touchscreen won't work?

    I dropped my iPhone 4s down the toilet because I saw a spider on the seat and jumped, causing me to drop it down the loo. I fished it out and dried it off. The phone was on so I tried to turn it off so it didn't fry the inside of it. I held down the

  • Automatically connecting to Wi-Fi

    If I've already setup a Wi-Fi connection in the iPhone, and I leave the area and come back, will the iPhone automatically connect to the available Wi-Fi or do I have to go into settings again and manually connect?

  • How to use the WRT54G2 as a switch...not a router?

    I've got one linksys wirless router that's providing DHCP addresses, doing MAC filtering, and allowing access to the internet (it's hooked up to our DSL modem).  The wireless signal doesn't reach to our other building (lots of brick and steel in the

  • File to IDOC aggsales mapping doubt Urgnt

    Hello, Im working for a retail client and the requirement is the File to IDOC scenario where Im stuck up in the mapping. The inbound is a flat file of structure EDI_DC40 E1WPU01 E1WPU02 E1WPU03 E1WPU04 The target IDOC structure is as follows WPUUMS01