Sorting a linkedList by the value of the object












1















For an assignment, I've been tasked to create a priority based support ticket system which contains the user's Name, ID, Handler and Priority however ticket's with higher priority are placed first in the list to be dealt with.



I have three classes.
Main: where I add/delete and change ticket priority.
TicketSystem: Contains the constructor for the ticket alongside getters and setter methods
LinkedList: Has insert, delete printList and should have sortList



So far I've determined the algorithm needs to be bubblesort as Priority is an int value but I'm not too sure how to receive the value for priority and then sort it.



public class TicketSystem {
private String handler;
private int priority;
private String iD;
private String creator;

public TicketSystem() {

}


public String getHandler ( ) {
return handler;
}

public int getPriority () {
return priority;
}

public String getID () {
return iD;
}
public String creator () {
return creator;
}

public void setID (String i) {

this.iD = i;

}
public void setHandler (String h) {
this.handler = h;
}
public void setPriority (int p ) {
this.priority = p;
}

public String setCreator (String c) {
return this.creator = c;
}



public void addTicket( String h, int p, String c, String iD) {
this.handler = h;
this.priority = p;
this.iD = iD;
this.creator = c;
}




@Override
public String toString() {
String output = "";
output += "Handler: " + handler +", ";
output += "Priority: " + priority + ", ";
output += "Creator: " + creator + ", ";
output += "ID: " + iD + " ";
return output;

}




}




public class LinkedList {
private Node head;
public LinkedList(TicketSystem ticket) {
head = new Node();
head.ticket = ticket;
head.link = null;
}


public boolean insertItem(TicketSystem ticket) {
Node n = new Node();
Node new_node;
new_node = head;
while (new_node.link != null) {
new_node = new_node.link;
}
n.ticket = ticket;
n.link = null;
new_node.link = n;
return true;

}

public void printList() {
Node z = head;
while (z!= null) {
System.out.println(z.ticket.toString());
z = z.link;
}
}

public boolean deleteItem(TicketSystem ticket) {
if(ticket.equals(head.ticket)) {
head = head.link;
return true;
} else {
Node prevNode = head;
Node curNode = head.link;
while(curNode != null && !(curNode.ticket == ticket)) {
prevNode = curNode;
curNode = curNode.link;

}
if(curNode != null) {
prevNode.link = curNode.link;
return true;
} else {
return false;
}
}

}

/* sort list */

public void sortList() {
TicketSystem ts = new TicketSystem();
}
class Node {
private TicketSystem ticket;
private Node link;
}
}









share|improve this question

























  • If you're looking for help with your LinkedList class, we'll need to see your LinkedList class.

    – mypetlion
    Nov 14 '18 at 23:48











  • Btw, ticket's with higher priority are placed first That is called a Priority Queue.

    – markspace
    Nov 14 '18 at 23:50











  • I've added in the linked list but should I research into Priority Queue if that's the solution? First time I've heard about it. Do you mind explaining how I could perhaps incorporate that element into my program?

    – Tea
    Nov 15 '18 at 0:39













  • @markspace is correct. Check docs.oracle.com/javase/7/docs/api/java/util/PriorityQueue.html and you will get all the details. You can find examples too on various sites Usually this happens with either students or new programmers that they don't know various ready made APIs... so I would suggest to search for keywords like in this case "priority and sorted collection" or something on that terms... that should give you options available in APIs... this is for next problem though

    – Ketan
    Nov 15 '18 at 1:06













  • Thank you so much, however my lecturer instructed us not to use pre-built libraries excluding array-list (though discouraged). Many of the student's I work with have been using Linked List and whilst a majority weren't able to add in a sort mechanism I know a few who did. I've got a way of adding it in but only within the Main method which seems impractical to me so I'm trying to find a way of doing it within the linked list. However, from what I'm getting at I'll need to switch data structures from Linked List to Priority Queue

    – Tea
    Nov 15 '18 at 4:48


















1















For an assignment, I've been tasked to create a priority based support ticket system which contains the user's Name, ID, Handler and Priority however ticket's with higher priority are placed first in the list to be dealt with.



I have three classes.
Main: where I add/delete and change ticket priority.
TicketSystem: Contains the constructor for the ticket alongside getters and setter methods
LinkedList: Has insert, delete printList and should have sortList



So far I've determined the algorithm needs to be bubblesort as Priority is an int value but I'm not too sure how to receive the value for priority and then sort it.



public class TicketSystem {
private String handler;
private int priority;
private String iD;
private String creator;

public TicketSystem() {

}


public String getHandler ( ) {
return handler;
}

public int getPriority () {
return priority;
}

public String getID () {
return iD;
}
public String creator () {
return creator;
}

public void setID (String i) {

this.iD = i;

}
public void setHandler (String h) {
this.handler = h;
}
public void setPriority (int p ) {
this.priority = p;
}

public String setCreator (String c) {
return this.creator = c;
}



public void addTicket( String h, int p, String c, String iD) {
this.handler = h;
this.priority = p;
this.iD = iD;
this.creator = c;
}




@Override
public String toString() {
String output = "";
output += "Handler: " + handler +", ";
output += "Priority: " + priority + ", ";
output += "Creator: " + creator + ", ";
output += "ID: " + iD + " ";
return output;

}




}




public class LinkedList {
private Node head;
public LinkedList(TicketSystem ticket) {
head = new Node();
head.ticket = ticket;
head.link = null;
}


public boolean insertItem(TicketSystem ticket) {
Node n = new Node();
Node new_node;
new_node = head;
while (new_node.link != null) {
new_node = new_node.link;
}
n.ticket = ticket;
n.link = null;
new_node.link = n;
return true;

}

public void printList() {
Node z = head;
while (z!= null) {
System.out.println(z.ticket.toString());
z = z.link;
}
}

public boolean deleteItem(TicketSystem ticket) {
if(ticket.equals(head.ticket)) {
head = head.link;
return true;
} else {
Node prevNode = head;
Node curNode = head.link;
while(curNode != null && !(curNode.ticket == ticket)) {
prevNode = curNode;
curNode = curNode.link;

}
if(curNode != null) {
prevNode.link = curNode.link;
return true;
} else {
return false;
}
}

}

/* sort list */

public void sortList() {
TicketSystem ts = new TicketSystem();
}
class Node {
private TicketSystem ticket;
private Node link;
}
}









share|improve this question

























  • If you're looking for help with your LinkedList class, we'll need to see your LinkedList class.

    – mypetlion
    Nov 14 '18 at 23:48











  • Btw, ticket's with higher priority are placed first That is called a Priority Queue.

    – markspace
    Nov 14 '18 at 23:50











  • I've added in the linked list but should I research into Priority Queue if that's the solution? First time I've heard about it. Do you mind explaining how I could perhaps incorporate that element into my program?

    – Tea
    Nov 15 '18 at 0:39













  • @markspace is correct. Check docs.oracle.com/javase/7/docs/api/java/util/PriorityQueue.html and you will get all the details. You can find examples too on various sites Usually this happens with either students or new programmers that they don't know various ready made APIs... so I would suggest to search for keywords like in this case "priority and sorted collection" or something on that terms... that should give you options available in APIs... this is for next problem though

    – Ketan
    Nov 15 '18 at 1:06













  • Thank you so much, however my lecturer instructed us not to use pre-built libraries excluding array-list (though discouraged). Many of the student's I work with have been using Linked List and whilst a majority weren't able to add in a sort mechanism I know a few who did. I've got a way of adding it in but only within the Main method which seems impractical to me so I'm trying to find a way of doing it within the linked list. However, from what I'm getting at I'll need to switch data structures from Linked List to Priority Queue

    – Tea
    Nov 15 '18 at 4:48
















1












1








1








For an assignment, I've been tasked to create a priority based support ticket system which contains the user's Name, ID, Handler and Priority however ticket's with higher priority are placed first in the list to be dealt with.



I have three classes.
Main: where I add/delete and change ticket priority.
TicketSystem: Contains the constructor for the ticket alongside getters and setter methods
LinkedList: Has insert, delete printList and should have sortList



So far I've determined the algorithm needs to be bubblesort as Priority is an int value but I'm not too sure how to receive the value for priority and then sort it.



public class TicketSystem {
private String handler;
private int priority;
private String iD;
private String creator;

public TicketSystem() {

}


public String getHandler ( ) {
return handler;
}

public int getPriority () {
return priority;
}

public String getID () {
return iD;
}
public String creator () {
return creator;
}

public void setID (String i) {

this.iD = i;

}
public void setHandler (String h) {
this.handler = h;
}
public void setPriority (int p ) {
this.priority = p;
}

public String setCreator (String c) {
return this.creator = c;
}



public void addTicket( String h, int p, String c, String iD) {
this.handler = h;
this.priority = p;
this.iD = iD;
this.creator = c;
}




@Override
public String toString() {
String output = "";
output += "Handler: " + handler +", ";
output += "Priority: " + priority + ", ";
output += "Creator: " + creator + ", ";
output += "ID: " + iD + " ";
return output;

}




}




public class LinkedList {
private Node head;
public LinkedList(TicketSystem ticket) {
head = new Node();
head.ticket = ticket;
head.link = null;
}


public boolean insertItem(TicketSystem ticket) {
Node n = new Node();
Node new_node;
new_node = head;
while (new_node.link != null) {
new_node = new_node.link;
}
n.ticket = ticket;
n.link = null;
new_node.link = n;
return true;

}

public void printList() {
Node z = head;
while (z!= null) {
System.out.println(z.ticket.toString());
z = z.link;
}
}

public boolean deleteItem(TicketSystem ticket) {
if(ticket.equals(head.ticket)) {
head = head.link;
return true;
} else {
Node prevNode = head;
Node curNode = head.link;
while(curNode != null && !(curNode.ticket == ticket)) {
prevNode = curNode;
curNode = curNode.link;

}
if(curNode != null) {
prevNode.link = curNode.link;
return true;
} else {
return false;
}
}

}

/* sort list */

public void sortList() {
TicketSystem ts = new TicketSystem();
}
class Node {
private TicketSystem ticket;
private Node link;
}
}









share|improve this question
















For an assignment, I've been tasked to create a priority based support ticket system which contains the user's Name, ID, Handler and Priority however ticket's with higher priority are placed first in the list to be dealt with.



I have three classes.
Main: where I add/delete and change ticket priority.
TicketSystem: Contains the constructor for the ticket alongside getters and setter methods
LinkedList: Has insert, delete printList and should have sortList



So far I've determined the algorithm needs to be bubblesort as Priority is an int value but I'm not too sure how to receive the value for priority and then sort it.



public class TicketSystem {
private String handler;
private int priority;
private String iD;
private String creator;

public TicketSystem() {

}


public String getHandler ( ) {
return handler;
}

public int getPriority () {
return priority;
}

public String getID () {
return iD;
}
public String creator () {
return creator;
}

public void setID (String i) {

this.iD = i;

}
public void setHandler (String h) {
this.handler = h;
}
public void setPriority (int p ) {
this.priority = p;
}

public String setCreator (String c) {
return this.creator = c;
}



public void addTicket( String h, int p, String c, String iD) {
this.handler = h;
this.priority = p;
this.iD = iD;
this.creator = c;
}




@Override
public String toString() {
String output = "";
output += "Handler: " + handler +", ";
output += "Priority: " + priority + ", ";
output += "Creator: " + creator + ", ";
output += "ID: " + iD + " ";
return output;

}




}




public class LinkedList {
private Node head;
public LinkedList(TicketSystem ticket) {
head = new Node();
head.ticket = ticket;
head.link = null;
}


public boolean insertItem(TicketSystem ticket) {
Node n = new Node();
Node new_node;
new_node = head;
while (new_node.link != null) {
new_node = new_node.link;
}
n.ticket = ticket;
n.link = null;
new_node.link = n;
return true;

}

public void printList() {
Node z = head;
while (z!= null) {
System.out.println(z.ticket.toString());
z = z.link;
}
}

public boolean deleteItem(TicketSystem ticket) {
if(ticket.equals(head.ticket)) {
head = head.link;
return true;
} else {
Node prevNode = head;
Node curNode = head.link;
while(curNode != null && !(curNode.ticket == ticket)) {
prevNode = curNode;
curNode = curNode.link;

}
if(curNode != null) {
prevNode.link = curNode.link;
return true;
} else {
return false;
}
}

}

/* sort list */

public void sortList() {
TicketSystem ts = new TicketSystem();
}
class Node {
private TicketSystem ticket;
private Node link;
}
}






java linked-list






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 23:54







Tea

















asked Nov 14 '18 at 23:43









TeaTea

62




62













  • If you're looking for help with your LinkedList class, we'll need to see your LinkedList class.

    – mypetlion
    Nov 14 '18 at 23:48











  • Btw, ticket's with higher priority are placed first That is called a Priority Queue.

    – markspace
    Nov 14 '18 at 23:50











  • I've added in the linked list but should I research into Priority Queue if that's the solution? First time I've heard about it. Do you mind explaining how I could perhaps incorporate that element into my program?

    – Tea
    Nov 15 '18 at 0:39













  • @markspace is correct. Check docs.oracle.com/javase/7/docs/api/java/util/PriorityQueue.html and you will get all the details. You can find examples too on various sites Usually this happens with either students or new programmers that they don't know various ready made APIs... so I would suggest to search for keywords like in this case "priority and sorted collection" or something on that terms... that should give you options available in APIs... this is for next problem though

    – Ketan
    Nov 15 '18 at 1:06













  • Thank you so much, however my lecturer instructed us not to use pre-built libraries excluding array-list (though discouraged). Many of the student's I work with have been using Linked List and whilst a majority weren't able to add in a sort mechanism I know a few who did. I've got a way of adding it in but only within the Main method which seems impractical to me so I'm trying to find a way of doing it within the linked list. However, from what I'm getting at I'll need to switch data structures from Linked List to Priority Queue

    – Tea
    Nov 15 '18 at 4:48





















  • If you're looking for help with your LinkedList class, we'll need to see your LinkedList class.

    – mypetlion
    Nov 14 '18 at 23:48











  • Btw, ticket's with higher priority are placed first That is called a Priority Queue.

    – markspace
    Nov 14 '18 at 23:50











  • I've added in the linked list but should I research into Priority Queue if that's the solution? First time I've heard about it. Do you mind explaining how I could perhaps incorporate that element into my program?

    – Tea
    Nov 15 '18 at 0:39













  • @markspace is correct. Check docs.oracle.com/javase/7/docs/api/java/util/PriorityQueue.html and you will get all the details. You can find examples too on various sites Usually this happens with either students or new programmers that they don't know various ready made APIs... so I would suggest to search for keywords like in this case "priority and sorted collection" or something on that terms... that should give you options available in APIs... this is for next problem though

    – Ketan
    Nov 15 '18 at 1:06













  • Thank you so much, however my lecturer instructed us not to use pre-built libraries excluding array-list (though discouraged). Many of the student's I work with have been using Linked List and whilst a majority weren't able to add in a sort mechanism I know a few who did. I've got a way of adding it in but only within the Main method which seems impractical to me so I'm trying to find a way of doing it within the linked list. However, from what I'm getting at I'll need to switch data structures from Linked List to Priority Queue

    – Tea
    Nov 15 '18 at 4:48



















If you're looking for help with your LinkedList class, we'll need to see your LinkedList class.

– mypetlion
Nov 14 '18 at 23:48





If you're looking for help with your LinkedList class, we'll need to see your LinkedList class.

– mypetlion
Nov 14 '18 at 23:48













Btw, ticket's with higher priority are placed first That is called a Priority Queue.

– markspace
Nov 14 '18 at 23:50





Btw, ticket's with higher priority are placed first That is called a Priority Queue.

– markspace
Nov 14 '18 at 23:50













I've added in the linked list but should I research into Priority Queue if that's the solution? First time I've heard about it. Do you mind explaining how I could perhaps incorporate that element into my program?

– Tea
Nov 15 '18 at 0:39







I've added in the linked list but should I research into Priority Queue if that's the solution? First time I've heard about it. Do you mind explaining how I could perhaps incorporate that element into my program?

– Tea
Nov 15 '18 at 0:39















@markspace is correct. Check docs.oracle.com/javase/7/docs/api/java/util/PriorityQueue.html and you will get all the details. You can find examples too on various sites Usually this happens with either students or new programmers that they don't know various ready made APIs... so I would suggest to search for keywords like in this case "priority and sorted collection" or something on that terms... that should give you options available in APIs... this is for next problem though

– Ketan
Nov 15 '18 at 1:06







@markspace is correct. Check docs.oracle.com/javase/7/docs/api/java/util/PriorityQueue.html and you will get all the details. You can find examples too on various sites Usually this happens with either students or new programmers that they don't know various ready made APIs... so I would suggest to search for keywords like in this case "priority and sorted collection" or something on that terms... that should give you options available in APIs... this is for next problem though

– Ketan
Nov 15 '18 at 1:06















Thank you so much, however my lecturer instructed us not to use pre-built libraries excluding array-list (though discouraged). Many of the student's I work with have been using Linked List and whilst a majority weren't able to add in a sort mechanism I know a few who did. I've got a way of adding it in but only within the Main method which seems impractical to me so I'm trying to find a way of doing it within the linked list. However, from what I'm getting at I'll need to switch data structures from Linked List to Priority Queue

– Tea
Nov 15 '18 at 4:48







Thank you so much, however my lecturer instructed us not to use pre-built libraries excluding array-list (though discouraged). Many of the student's I work with have been using Linked List and whilst a majority weren't able to add in a sort mechanism I know a few who did. I've got a way of adding it in but only within the Main method which seems impractical to me so I'm trying to find a way of doing it within the linked list. However, from what I'm getting at I'll need to switch data structures from Linked List to Priority Queue

– Tea
Nov 15 '18 at 4:48














0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53310429%2fsorting-a-linkedlist-by-the-value-of-the-object%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53310429%2fsorting-a-linkedlist-by-the-value-of-the-object%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Florida Star v. B. J. F.

Danny Elfman

Lugert, Oklahoma