• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

Sort list of list by first element

 
Greenhorn
Posts: 9
Python C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a little bit of a problem, I don't know how to sort a list of lists by their first eements.

List: [[4, Taeriel, Mondull], [6, Elegast, Mondull], [2, Taeriel, Boltor]]

sorted list: [[6, Elegast, Mondull], [4, Taeriel, Mondull], [2, Taeriel, Boltor]]

Can anybody help me please?
 
Marshal
Posts: 74341
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Of course we can help you; it helps you a lot more to guide you work it out for yourself. What do you know about sorting?

And . . . welcome to the Ranch
 
Sander Trepp
Greenhorn
Posts: 9
Python C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know nothing I've learned java for like three days because of a internship program, they gave me a task and I'm trying to complete it.
 
Campbell Ritchie
Marshal
Posts: 74341
334
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What have you learnt about Lists? What sort of element does that List contain? Have you got any books? Have you found any tutorials? Look at this part of the Java™ Tutorials.
 
lowercase baba
Posts: 13013
66
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
At a very high level, it doesn't matter that you have "a list of lists".  You could have a list of anything, and the concept is the same.  You need to look at two elements of your list, and determine which one should be before the other.  
 
Sander Trepp
Greenhorn
Posts: 9
Python C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How to create a list, add items or lists into them. In that list that I want to sort has one int, two str and I wanto to sort it by the integral. I have watches some tutorials, but those teach too slow so I've just looked stackoverflow for any problems I've had.
 
Saloon Keeper
Posts: 1603
51
Eclipse IDE Postgres Database C++ Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From the little you have shown it would appear that you have a List of Lists of 3 elements.

Additionally, it would appear that those 3 elements include what would probably seem to be an int and (probably) two String elements.

While this is legal in Java for legacy reasons (compatibility with code from 1995) this would not be considered normal or proper coding style in Modern Java.

More likely, the List of 3 elements should itself be considered a class of its own.

If that is true, and you always wish to sort by the integer, because that is "natural" for that type, you can have that class implement Comparable and define a .compareTo() for it that will compare based on the int value.

It is not impossible to sort a List of Lists by the first element of the contained List, what I might term "Python-style", but that is likely not the right solution.

Was it given as part of your assignment that the correct way to represent the elements of the List is as Lists of 3 elements?

Again, that is not illegal per se, but a design that would not normally be done in Modern Java.
 
Campbell Ritchie
Marshal
Posts: 74341
334
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sander Trepp wrote:. . . I have watches some tutorials

Unfortunately there are only too many bad tutorials on the Net

but those teach too slow . . .

Most videos I have watched make the opposite mistake, teaching too fast, or assuming the viewers already know more than they do. I think it is usually better to take things slowly.
 
Sander Trepp
Greenhorn
Posts: 9
Python C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Sander Trepp wrote:. . . I have watches some tutorials

Unfortunately there are only too many bad tutorials on the Net

but those teach too slow . . .

Most videos I have watched make the opposite mistake, teaching too fast, or assuming the viewers already know more than they do. I think it is usually better to take things slowly.



But I need to learn fast and get this internship task completed in 4 days or I will not make it.

If somebody wants to help me code this(I attached the task) I would be very grateful

Perfect Dating Site



Build the perfect matchmaking site!  Being perfect, it doesn’t have to be [completely] real, e.g. the sole goal is matching, not engagement.

Your task is to build a site that will break into the booming fantasy dating scene. This particular site caters to a very niche audience: misfit elves and dwarves only looking to date outside of their own race - Elves & Dwarves Misfits - “EDM”. “EDM” offers elves and dwarves enthralling encounters with one another. After such an encounter, each dwarf and elf will grade their encounter by swinging their axe or knife (respectively) into The Sorting Tree. Strikes into the right side of the tree indicate a positive encounter, while marks on the left side of the tree indicate a negative encounter. A greater number of strikes indicates a stronger feeling after the encounter.

Given E number of elves and D number of dwarves, D <= E <= 500, the task is to optimize the creation of stable couples through “EDM”. “EDM” defines a stable couple as one where simultaneously no elf or dwarf would: a] like to leave their current relationship for someone else that they like more, and b] there is no external party that would pair better to one of the individuals already in the relationship (i.e. that no one else would want them more than their current partner).


The task should be a complete Java program reading from the standard input and writing to the standard output.
Input:

1)Names of the elves: a line starting with “Elves:”, followed by a comma separated list of unique elven names. The names consist only of lower and upper case Latin characters, no special symbols.
2)Names of the dwarves: a line starting with “Dwarves”, followed by a comma separated list of unique dwarven names. The names consist only of lower and upper case Latin characters, no special symbols.
3)Encounters between elves and dwarves and their swings onto a tree in the format: elfName :  dwarfName = #elfinSwing SwingType x #dwarvenSwing SwingType, e.g. Ave : Boltor = 3R x 1L. In that example Ave likes Boltor quite a bit (3Right knife swings) but Boltor doesn’t like Ave back (1Left axe swing)
*SwingType can be either “R” or “L”
*Each elf can meet each dwarf at most once. Hence, the maximum amount of encounters is the product of E and D.
*Only elves and dwarves who have met each other can form a couple.
4)An empty line denotes the end of the input.


Output:

1)All stable relationships in the format of elfName :  dwarfName, followed by a new line


_________________

Sample Input:

Elves: Ave, Elegast, Taeriel, Saeya, Loralf

Dwarves: Duthila, Mondull, Boltor, Drummer

Ave : Duthila = 1R x 4R

Elegast : Duthila = 1R x 1R

Taeriel : Duthila = 1L x 3L

Saeya : Duthila = 2R x 1L

Loralf : Duthila = 1R x 2R

Taeriel : Mondull = 1R x 3R

Elegast : Mondull = 1R x 5R

Taeriel : Boltor = 1R x 1R

Loralf : Boltor = 1R x 1R

Loralf : Drummer = 1R x 1R

Loralf : Mondull = 2R x 2R

Elegast : Boltor = 1R x 10R

Saeya : Mondull = 1L x 1L

Saeya : Boltor = 1R x 1L


Sample Output:

Ave : Duthila

Elegast : Mondull

Taeriel : Boltor

Loralf : Drummer




____________________

Notes:

The task requires no special libraries outside the JDK, so use of such libraries is discouraged. It’s possible to have more than a single ‘stable’ solution.

The provided package contains two test input files: “test1” and “test2”, and two sample outputs “output1” and “output2”.


While the presented task is about fantasy couples, dating, and weapon-swinging, the matching problem has a wider application - in 2012 Nobel Prize in Economic Sciences was awarded to Lloyd S. Shapley and Alvin E. Roth: https://www.nobelprize.org/prizes/economic-sciences/2012/summary/


Good luck!


 
Campbell Ritchie
Marshal
Posts: 74341
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's a bigger task than you suggested at first.
I suggest you start by describing a part of the task, the smaller the better, and how it would work. Then write code to implement that.
 
Sander Trepp
Greenhorn
Posts: 9
Python C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know I just had problems sorting lists
 
Marshal
Posts: 26909
82
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Keeping my eyes closed so that I don't see the actual complexity of what you're supposed to do:

If you have a List of List objects, and you want to sort the outer list based on some attribute of the inner lists, then one way to do that is to use the List.sort(Comparator) method. You'll need to write a Comparator which compares two of the inner list objects and returns the appropriate value to say whether the first object is "less than" the second, "equal to" it, or "greater than" it.

I haven't written out the actual Java code including the generic types because you haven't said what they might be, but anyway that would be how I would start out, given what you have told us.
 
Marshal
Posts: 5112
319
IntelliJ IDE Python Java Linux
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wowsers! 4 days for all of that? You're going to be busy.
 
Sander Trepp
Greenhorn
Posts: 9
Python C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Cooke wrote:Wowsers! 4 days for all of that? You're going to be busy.



Do you want to help me? I've been programming for like 3 months, only on python and now I've been learning java for 4days
 
Campbell Ritchie
Marshal
Posts: 74341
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We are trying our hardest to help. Giving you a solution wouldn't count as help.
 
Sander Trepp
Greenhorn
Posts: 9
Python C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes I get it, I'm just stuck and don't know how to do this task properly.
 
Tim Cooke
Marshal
Posts: 5112
319
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let's start at the beginning. Do you have a Java program that compiles and runs?

It doesn't have to do anything, even just printing "Hello World" to the terminal would suffice for this step.
 
Campbell Ritchie
Marshal
Posts: 74341
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you can't do what Tim suggested, please go to the Java™ Tutorials link I gave you yesterday and find its very first part, about getting started.
 
Sander Trepp
Greenhorn
Posts: 9
Python C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know already basic thing about java and how classes etc work. I just found a course that is pretty good and helpful.
 
Campbell Ritchie
Marshal
Posts: 74341
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sander Trepp wrote:. . . a course that is pretty good . . .

Which course?

Have you decided what sort of classes you need? How are you writing them? What algorithm are you going to use? Hint: the link about Shapley and Roth should give you a starting place to search from.
 
Tim Cooke
Marshal
Posts: 5112
319
IntelliJ IDE Python Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My question still stands though. Do you have a Java program that compiles and runs?

Understanding how classes work in theory is not the same as having working software. I'm trying to understand exactly where you are in your Java journey so any help and advice I give will be relevant and useful to you.
 
Sander Trepp
Greenhorn
Posts: 9
Python C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes
 
reply
    Bookmark Topic Watch Topic
  • New Topic